Query from Previous Query

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

Query from Previous Query

Post by AliasBDI »

Is there a way to have two queries. One queries the result from a column into itself? In other words:

QUERY 1: Queries a 'registration' table that has the column 'userid'.

QUERY 2: Queries a 'users' table with the QUERY 1 result for 'userid' in order to result that particular user's information which was in the 'registration' table.

Make sense? If that is possible, what would it look like? Both queries will have multiple records result.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

that sure sounds like a join...

Code: Select all

SELECT b.* FROM table1 a INNER JOIN table2 b ON a.userid = b.userid
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

Post by AliasBDI »

Cool. I began coding it when I realized the INNER JOIN was the way to go. But there is more to it than I thought. There is actually 3 tables involved. TABLE1 has the 'userid' for the query in TABLE2, but I need to show TABLE3 result which has the 'userlevel' query by TABLE2. In other words:

I call up TABLE1.
TABLE1 sends a field result to query TABLE2.
TABLE2 sends a field result to query TABLE3.
I echo TABLE3 result.

It is a bit strange to explain, but that is it. TABLE1 and TABLE3 do not have any common fields except through TABLE2. What about that?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

more inner joins.. you can stack as many as you like..
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

Post by AliasBDI »

Like this?

Code: Select all

SELECT b.* FROM table1 a INNER JOIN table2 b ON a.userid = b.userid, table3 c ON b.id = c.id
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

read your manual for the syntax of multi-joins..

[mysql_man]join[/mysql_man]
[pg_man]join[/pg_man]
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

Post by AliasBDI »

Will do. Thanks. http://www.postgresql.org was a BIG help.
Post Reply