| Query [message #569832] |
Fri, 02 November 2012 07:10  |
 |
UZUMAKI
Messages: 7 Registered: October 2012
|
Junior Member |
|
|
Hello,
Once again you've been asking for help for a query. To a query I want to do is needed 3 tables, Table1, Table2 and Table3. In this Table 1 contains ID and NAME in this Table 2 contains ID, NAME, and ID_TABLE1 and Table 3 contains this ID, NAME, and ID_TABLE2.
The problem here is that I only get the ID Table1 and through it I have to go get all the information from the other tables. There is a possibility of doing it all in one query?
|
|
|
|
| Re: Query [message #569833 is a reply to message #569832] |
Fri, 02 November 2012 07:14   |
cookiemonster
Messages: 9287 Registered: September 2008 Location: Rainy Manchester
|
Senior Member |
|
|
Looks simple enough, have you tried writing a query that joins the three tables?
If not, do so.
If you get stuck post what you tried.
|
|
|
|
| Re: Query [message #569835 is a reply to message #569833] |
Fri, 02 November 2012 07:24   |
 |
UZUMAKI
Messages: 7 Registered: October 2012
|
Junior Member |
|
|
What I did was split the query into two, since I need to get ID_TABLE2 get information from table 3. Then ask for help, I try to do everything and I am not in a can.
SELECT * FROM (SELECT TABLE1.ID, TABLE1.NAME, TABLE2.ID, TABLE2.NAME FROM TABLE1, TABLE2
WHERE TABLE1.ID = TABLE2.ID ORDER BY NAME) WHERE ID_TABLE1= $_GET[id]
SELECT * FROM (SELECT TABLE2.ID, TABLE2.NAME, TABLE3.NAME FROM TABLE2, TABLE3
WHERE TABLE2.ID = TABLE3.ID ORDER BY NAME) WHERE ID_TABLE2= $_GET[id]
|
|
|
|
| Re: Query [message #569838 is a reply to message #569835] |
Fri, 02 November 2012 07:58  |
cookiemonster
Messages: 9287 Registered: September 2008 Location: Rainy Manchester
|
Senior Member |
|
|
Please read and follow How to use [code] tags and make your code easier to read?
The above selects are both invalid. You can't reference id_table1 in the outer query unless you select it in the inner query. Same for id_table2.
I would assume, based on the column names that table1.id joins to table2.id_table1 not table2.id as you have done.
I also don't see why there is any need for a sub-select here.
What's wrong with writing the query in the form:
SELECT <columns>
FROM table1, table2, table3
WHERE table1.column = table2.column
AND table2.column = table3.column
|
|
|
|