gpt4 book ai didi

mysql - 如果结果/行不存在,请选择列?

转载 作者:行者123 更新时间:2023-11-29 07:59:36 25 4
gpt4 key购买 nike

我试图根据另一列中是否存在 user_id 来从一列中选择 post_ids 列表。

这个想法只是获取用户尚未与之交互的 post_ids 列表。

表格内容如下:

+----+---------+---------+--------+---------------------+------------+------+
| id | post_id | user_id | rating | last_update | num_images | url |
+----+---------+---------+--------+---------------------+------------+------+
| 1 | 2938 | 5 | 1 | 2014-06-12 22:54:31 | NULL | null |
| 2 | 2938 | 1 | 1 | 2014-06-12 22:54:54 | NULL | null |
| 3 | 2940 | 6 | 1 | 2014-06-12 23:36:25 | NULL | null |
| 4 | 2943 | 3 | 0 | 2014-06-12 23:39:29 | NULL | NULL |
+----+---------+---------+--------+---------------------+------------+------+

我的尝试是这样的:

SELECT Distinct post_id
FROM `table`
WHERE user_id !=1

然而,我仍然得到的结果仍然给出了用户已经与帖子连接的结果——它只是排除了包括用户在内的条目。

在user_id没有与比较列中post_id的任何实例关联的情况下,如何获取结果?

最佳答案

您的查询应该是:

select distinct(post_id )
from tableName
where post_id not in(select post_id from tableName where user_id=1);

关于mysql - 如果结果/行不存在,请选择列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24202559/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com