在插入查询中添加多个条件语句

时间:2013-07-12 11:00:11

标签: mysql sql

为了确保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
)

知道如何在插入过程中指定(多个)条件吗?

感谢 帕特里克

1 个答案:

答案 0 :(得分: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