我有这样的表:
map table1 table2 table3
| aID | bID | | bid | val | | bid | val | | bid | val |
------------- ------------- ------------- -------------
| 1 | 1 | | 1 | 20 | | 3 | 10 | | 4 | 15 |
------------- ------------- ------------- -------------
| 1 | 2 | | 1 | 22 | | 3 | 23 | | 4 | 25 |
------------- ------------- ------------- -------------
| 1 | 3 | | 2 | 15 | | 3 | 14 | | 4 | 11 |
------------- ------------- ------------- -------------
| 2 | 3 | | 2 | 23 | | 5 | 32 | | 2 | 3 |
-------------
| 2 | 4 |
-------------
像这样尝试:
SELECT val, bid
FROM map m
RIGHT JOIN table1 t
ON m.bID =t.bid
RIGHT JOIN table2 tt
ON m.bID =tt.bid
RIGHT JOIN table3 ttt
ON m.bID =ttt.bid
WHERE aID = 1
bID可以只是在tableX之一中
有没有一种简单的方法来检索aID 1的所有值?
最佳答案
试试这个查询
SELECT val, bid
FROM map m
LEFT JOIN table1 t
ON m.bID =t.bid
LEFT JOIN table2 tt
ON m.bID =tt.bid
LEFT JOIN table3 ttt
ON m.bID =ttt.bid
WHERE aID = 1
好的,根据您的评论,我编辑了我的帖子,尝试一下
SELECT val, bid
FROM map m
LEFT JOIN table1 t
ON m.bID =t.bid
WHERE aID = 1
UNION
SELECT val, bid
FROM map m
LEFT JOIN table2 tt
ON m.bID =tt.bid
WHERE aID = 1
UNION
SELECT val, bid
FROM map m
LEFT JOIN table3 ttt
ON m.bID =ttt.bid
WHERE aID = 1
关于mysql - mysql关联表到多个表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21723074/