samedi 9 juillet 2016

mysql - Join tables based on multiple columns in second

I have two tables Place and Post which i need to generate an output

place
--------------
std_id  sch_id
1       1
2       2
3       1
4       3
5       4
6       2
7       5
8       3
9       5
10      4

and

Post
------------
sch1_id  sch2_id  sch3_id  sup_id
1        2        3        1
3        2        4        2

I need to generate the following output

sup_id  sch_id  std_id
1       1       1
1       1       3
1       2       2
1       2       6
1       3       4
1       3       8
2       3       4
2       3       8
2       2       2
2       2       6
2       4       5
2       4       10

I tried this

 SELECT pl.std_id,po.sup_id FROM `place` pl,`post` po WHERE pl.sch_id =
 po.sch1_id group by po.sup_id

and got this

| std_id | sup_id |
|--------|--------|
|      1 |      1 |
|      4 |      2 |

EDIT

SQLFiddle

Aucun commentaire:

Enregistrer un commentaire