我需要有关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  |
+----+---------+--------+


ID 1、7、8、14中的userId“ kalle”已读取topicId 1

ID为4,5,6,11,12,15的userId“ kalle”已读取topicId 2

然后我也想在topicId中获得唯一的值,id 13或17可以显示,但不能同时显示。

我希望你明白我的追求

最佳答案

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
)

关于mysql - 论坛MySQL查询,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26022981/

10-11 03:24