Page 1 of 1

Query from Previous Query

Posted: Sat Sep 18, 2004 11:05 pm
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.

Posted: Sat Sep 18, 2004 11:09 pm
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

Posted: Sat Sep 18, 2004 11:17 pm
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?

Posted: Sat Sep 18, 2004 11:19 pm
by feyd
more inner joins.. you can stack as many as you like..

Posted: Sat Sep 18, 2004 11:25 pm
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

Posted: Sat Sep 18, 2004 11:27 pm
by feyd
read your manual for the syntax of multi-joins..

[mysql_man]join[/mysql_man]
[pg_man]join[/pg_man]

Posted: Sat Sep 18, 2004 11:50 pm
by AliasBDI
Will do. Thanks. http://www.postgresql.org was a BIG help.