![]() |
The art of sub-selects
Sub-Selects are one of the most powerful new(ish) additions to the mysql database. They are supported from mysql 4.1 onward.
Code:
SET @v1 = '';Code:
SELECT * FROM table2 WHERE column2 = (SELECT column_id FROM table1 WHERE column='blah' LIMIT 1);Now for some more advanced stuff: Using a sub-select query as a table: Code:
SELECT Using Sub-select in an "IN" where statement: Code:
SELECTCode:
table1.ref_id IN (1,2,3,4,5,6,7,8,9,10) |
Isn't it @myVar:= and not $v1:=? In any case, I do like the way you do it for the sub-selects, but I also like the way variables can make things much more readable.
For instance: Code:
SELECT |
Nice post Bluesaga, good to see people contributing to the MySQL forum :)
|
Thanks for the kind words Karl.
And thanks for the typo-catch Adam! However for your peice of code, why wouldn't you just do: Code:
SELECT |
| All times are GMT. The time now is 09:44 PM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0