-   Programming (
-   -   mysql use output of one query in another query (

secretlydead 11-18-2007 06:44 AM

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?

jlinkels 11-18-2007 07:18 AM

If you are using mysql > 4.2 you can use a select statement in the where clause. Check 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:

SELECT field, outcome, othervalue FROM table1 JOIN table2 ON table1.outcome=table2.othervalue WHERE table1.field= '2'
All the fields I mention behind the SELECT are for illustration purposes, you can show whatever you need.


chrism01 11-19-2007 02:25 AM

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 08:51 AM.