0

To ensure some form of referential integrity in Myisam (Mysql) I am trying to do updates and insert using subqueries. For example for updating I use this:

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)

However the same principle for inserts doesn't work; it tried something like this:

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
)

Any idea how to specify (multiple) conditions during insert?

thanks Patrick

1
  • @Strawberry What's wrong with that syntax? Seems ok to me. Commented Jul 12, 2013 at 11:14

1 Answer 1

1

Consider this approach:

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
Sign up to request clarification or add additional context in comments.

2 Comments

INSERT ... SELECT is probably the way to go. dev.mysql.com/doc/refman/5.1/en/insert-select.html
@user2022678 If it worked, please accept this answer by pressing the "tick" at its left.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.