2016-01-05 24 views
0

當前語法:需要插入查詢修改爲accomdate多個值

insert into log_file 
(
    table_id, 
    text, 
    today_date, 
    user_id 
) 
values 
(
(select id from table where table_no in ('Table1','Table2')), 
"Some Text Now", 
now(), 
143 
); 

當然,我得到的錯誤 'ERROR 1242(21000):子查詢返回多個1行的'

我怎麼能accomdate這而不必寫出很多個人查詢?

+0

的可能的複製(HTTP:// stackoverflow.com/questions/9422529/mysql-how-do-you-insert-into-a-table-with-a-select-subquery-returning-multiple-r) –

回答

1

無需使用子查詢,你可以使用INSERT...SELECT語法:[?MySQL的你怎麼插入表與SELECT子查詢返回多個行]

insert into log_file (table_id, text, today_date, user_id) 
select id, "Some Text Now", now(), 143 
from table 
where table_no in ('Table1','Table2')