qadenza
qadenza

Reputation: 9293

copy certain column values from one table to anther

In both tables I have a column named date (datetime - current timestamp).
I need to copy the first 20 values of that column from table posts to table banners.

insert into banners set date SELECT date FROM posts WHERE id < 21;
update banners set date SELECT date FROM posts WHERE id < 21;

In both cases I have an error:
Column count doesn't match value count at row 1

What does it mean this error and how I can copy the values?

Upvotes: 1

Views: 45

Answers (1)

Ullas
Ullas

Reputation: 11556

Use JOIN with UPDATE.

Query

update `banners` t1
from `posts` t2
on t1.`id` = t2.`id`
set t1.`date` = t2.`date`
where t2.`id` < 21;

Upvotes: 1

Related Questions