falco3205 falco3205 -3 years ago 278
MySQL Question

SELECT * FROM table1, table2, table3

I have a problem ...
In table1 I have an id, I have to compare that id in table2, then fetch the second id that is in table2 and compare it to table3 and get as a result a datum.
Example

TABLE1
ID NAME ECC...
1 Jhon
2 Frank

TABLE2
ID ID2 ECC..
1 4
2 8

TABLE3
ID NAME
4 Sea
8 Hello


If I look for id 1, the result must be Sea
If I look for id 2 the result must be Hello

Thanks!

Answer Source

You should use joins.

Your query will look like :

SELECT t3.name
FROM table3 t3
LEFT JOIN table2 t2 ON t3.id = t2.id2
LEFT JOIN table1 t1 ON t2.id2 = t1.id
WHERE t1.id = <your_number>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download