2013-07-12 79 views
0

爲了確保Myisam(Mysql)中的某種形式的參照完整性,我嘗試使用子查詢進行更新和插入操作。例如更新我使用這個:在插入查詢中添加多個條件語句

update tbl_a 
set col_a='test' 
where ID=22 and '2' IN (SELECT ID FROM tbl_b) and '33' IN (SELECT ID FROM tbl_c) 

但是插入的相同原則不起作用;它嘗試了這樣的事情:

insert into tbl_a 
(
a, 
b, 
c 
) 
values 
(
    now(), 
    select ID from tbl_b where ID=2, 
    select ID from tbl_c where ID=23 
) 

任何想法如何在插入過程中指定(多個)條件?

感謝 帕特里克

+0

@Strawberry什麼是錯的語法?對我來說似乎沒問題。 –

回答

1

考慮使用此方法:

insert into tbl_a (a, b, c) 
select now(), b.id, c.id from tbl_b b, tbl_c c where b.id=2 and c.id=23 
+0

INSERT ... SELECT可能是要走的路。 http://dev.mysql.com/doc/refman/5.1/en/insert-select.html –

+0

謝謝;有效! :-) – user2022678

+0

@ user2022678如果有效,請按左側的「打勾」接受此答案。 – Vesper