LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

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

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:

2
5
7
8

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
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Wheezy/Jessie/Sid, Linux Mint DE
Posts: 4,192

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

jlinkels
 
Old 11-19-2007, 02:25 AM   #3
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,311

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

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


Reply


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


All times are GMT -5. The time now is 03:38 PM.

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