John Carlo Cahimat John Carlo Cahimat - 3 months ago 8
MySQL Question

Mysql - How to query a row from table _a that is link to table_b pointing to a different a row in table_a

For Example:

table_a

table_a
|key|Value|
|---|-----|
| 2 |john |
|---|-----|
| 3 |james|
-----------


and table_b

table_b
|key|key2 |
|---|-----|
| 2 |3 |
|---|-----|
| 9 |5 |
-----------


Output:

desired_tbl
|key|Value|
|---|-----|
| 2 |james|
-----------


Its like a self referencing table but in a very different way...

I hope you can help me. thank you... :)

Answer

SELECT a1.key, a2.value FROM table_a as a1, table_b as b, table_a as a2 WHERE a1.key = b.key AND b.key2 = a2.key;

SQL Fiddle - http://sqlfiddle.com/#!9/f581c/24

Comments