我需要有关sql查询的帮助
我有这张桌子(post_table):
+----+---------+--------+
| id | topicId | userId |
+----+---------+--------+
| 1 | 1 | kalle |
| 2 | 1 | pelle |
| 3 | 2 | laban |
| 4 | 2 | kalle |
| 5 | 2 | kalle |
| 6 | 2 | kalle |
| 7 | 1 | kalle |
| 8 | 1 | kalle |
| 9 | 2 | laban |
| 10 | 1 | laban |
| 11 | 2 | kalle |
| 12 | 2 | kalle |
| 13 | 3 | laban |
| 14 | 1 | kalle |
| 15 | 2 | kalle |
| 16 | 1 | kalle |
| 17 | 3 | laban |
| 18 | 4 | laban |
| 19 | 5 | laban |
| 20 | 6 | pelle |
+----+---------+--------+
我想只获得userId“kalle”没有阅读帖子的行,它们是
+----+---------+--------+
| id | topicId | userId |
+----+---------+--------+
| 13 | 3 | laban |
| 17 | 3 | laban |
| 18 | 4 | laban |
| 19 | 5 | laban |
| 20 | 6 | pelle |
+----+---------+--------+
topicId 1已被idI 1,7,8,14
中的userId“kalle”读取topicId 2已被userId“kalle”读取,内容为id 4,5,6,11,12,15
然后我还希望获得topicId中的唯一值,id 13或17可以显示但不能同时显示。
我希望你明白我在追求什么
答案 0 :(得分:0)
SELECT * FROM POST_TABLE WHERE ID IN
(
SELECT MAX(ID) FROM POST_TABLE WHERE TOPICID NOT IN
(
SELECT DISTINCT TOPICID FROM POST_TABLE WHERE USERID ='kalle'
)
GROUP BY TOPICID
)