我有2张这样的表:
表人
id | name
---------
1 | john
2 | mike
3 | carl
4 | keny
5 | anna
表车
owner | vechicle
----------------
1 | RTA457
3 | GSW684
3 | GKI321
3 | SNE798
5 | YTT662
所以,我想创建一个连接两个表的查询,如下所示:
SELECT * FROM person LEFT JOIN vehicle ON person.id=vehicle.owner
获得这些结果
id | name | owner | vechicle
----------------------------
1 | john | 1 | RTA457
2 | mike | NULL | NULL
3 | carl | 3 | GSW684
3 | carl | 3 | GKI321
3 | carl | 3 | SNE798
4 | keny | NULL | NULL
5 | anna | 5 | YTT662
最后,我想将它限制为3人,显示他们所有的车辆,如下:
id | name | owner | vechicle
----------------------------
1 | john | 1 | RTA457
2 | mike | NULL | NULL
3 | carl | 3 | GSW684
3 | carl | 3 | GKI321
3 | carl | 3 | SNE798
有办法吗?
答案 0 :(得分:2)
可能有助于子查询
SELECT
*
FROM
(SELECT * FROM person LIMIT 3) t
LEFT JOIN vehicle ON t.id = vehicle.owner
答案 1 :(得分:1)
没有尝试过,但是这样的事情:
SELECT * FROM Users LEFT JOIN OtherUserData ON Users.userId = OtherUserData.userId;
答案 2 :(得分:0)
您可以简单地查询:
SELECT * FROM person LEFT JOIN vehicle ON person.id=vehicle.owner LIMIT 10;
此SO也可以派上用场。希望这有帮助!