Review your favorite Linux distribution.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 11-18-2007, 06:44 AM   #1
Registered: Sep 2003
Location: Qingdao, China
Distribution: mandriva, slack, red flag
Posts: 249

Rep: Reputation: 31
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?
Old 11-18-2007, 07:18 AM   #2
LQ Guru
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,191

Rep: Reputation: 1039Reputation: 1039Reputation: 1039Reputation: 1039Reputation: 1039Reputation: 1039Reputation: 1039Reputation: 1039
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.

Old 11-19-2007, 02:25 AM   #3
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.10, Centos 7.5
Posts: 17,680

Rep: Reputation: 2490Reputation: 2490Reputation: 2490Reputation: 2490Reputation: 2490Reputation: 2490Reputation: 2490Reputation: 2490Reputation: 2490Reputation: 2490Reputation: 2490
Here's a sub-select if your DB allows it:

select a.x from a where a.y in (select b.q from b)


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I output the result of a query in php? orfiyus Programming 0 07-19-2007 03:33 PM
help with mysql query: return nth rows in query hawarden Programming 2 07-31-2006 07:36 PM
Mysql Error: Lost Connection to Mysql during query ramnath Programming 5 11-18-2003 01:27 PM
maybe NOT so simple an output query dougp Linux - General 1 09-05-2002 09:42 PM
a simple text output query dougp Linux - Newbie 1 09-05-2002 11:45 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:05 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration