mysql use output of one query in another query
Does anyone know how to do this without using php (can you put it all in one mysql query):
I'm grabbing some numbers from a database. The first query output looks something like this:
query is like: select something from somewhere where field = '2';
Then I need to run another query:
select something from somewhere else where field = 'result of the last query output';
I can do this with php, but i'm wondering if there's a one shot way to do it through mysql?
If you are using mysql > 4.2 you can use a select statement in the where clause. Check mysql.com documentation on this, I have always been using mysql 4.0 so I am not sure about the syntax.
ALternatively you might join the two or (more) tables and do the combination in the join statement.
Suppose you have table1 and table2. Table1 contains your field which can have the value '2'. Table1 also has the column outcome, which value you want to use in table2 to find other values. Say Table2 contains a column 'othervalue'.
Then the SQL statement would be:
Here's a sub-select if your DB allows it:
select a.x from a where a.y in (select b.q from b)
|All times are GMT -5. The time now is 03:58 AM.|