Page 1 of 1

INSERT and SELECT together in one statement

Posted: Tue Jun 12, 2007 9:57 am
by superdezign
Is it possible to do something like this:

Code: Select all

INSERT INTO `newFeature` (`userid`, `data`, ...) VALUES((SELECT `id` FROM `users` WHERE `username` = '$username' AND `password` = '$password'), $data, ...);
... without doing the select statement separately?


It's not crucial or anything... I've already got a way around it. I'm just curious.

Posted: Tue Jun 12, 2007 10:03 am
by JayBird
Yes, it is totally possible. Need MySQL >= 4.1

http://dev.mysql.com/doc/refman/5.0/en/subqueries.html

Posted: Tue Jun 12, 2007 10:26 am
by superdezign
I have MySQL 5, and I can't use subqueries with the INSERT statement. Using them in a SELECT statement, however, work's fine.

Posted: Tue Jun 12, 2007 7:35 pm
by aaronhall
You're looking for this

Posted: Tue Jun 12, 2007 8:27 pm
by superdezign
^_^

Thank ya.