1
PendingOni 2022-11-04 01:54:51 +08:00 1
A 去 left join B?
```SQL SELECT A.ItemId, Description, CreatedAt, B.GroupTitle FROM items A left join groups B on A.GroupId = B.GroupId WHERE UserId = 1 ORDER BY ItemId ``` |
2
kkwa56188 2022-11-04 03:30:24 +08:00 1
-- 也可以用 子查询. 这里用了个 max()是怕 B.GroupId 不唯一
SELECT A.ItemId, A.Description, A.CreatedAt , ( select max(B.GroupTitle) from groups B WHERE B.GroupId = A.GroupId ) FROM items A AND A.UserId = 1 ORDER BY A.ItemId; |
3
akira 2022-11-04 06:48:14 +08:00 1
`
SELECT A.ItemId, A.Description, A.CreatedAt, B.GroupTitle FROM items A left join groups B on A.GroupId = B.GroupId WHERE 1=1 AND UserId = 1 ORDER BY ItemId ` |
4
xuelu520 2022-11-04 10:57:38 +08:00 1
left join 就行。
楼主可以去看看 left/right/inner 几种的区别,几种 join 用的还是很多的。 |
5
edis0n0 OP 成功了,谢谢大家
|