gpt4 book ai didi

mysql - 在 MySQL 查询中同时使用存在和简单值检查

转载 作者:行者123 更新时间:2023-11-29 23:20:45 24 4
gpt4 key购买 nike

我有以下 MySQL 查询:

SELECT * FROM posts p WHERE
EXISTS (
SELECT 1 FROM following f WHERE f.from = 2 AND f.to = p.uid
)
OR p.uid = 2

我希望它返回来自 posts 的所有记录,其中 following 有一条记录 WHERE f.from = 2 AND f.to = p.uid 以及 p.uid = 2 的所有记录。

如果我先进行存在检查,则仅返回与存在匹配的记录。如果我交换它们,则仅返回与 p.uid = 2 匹配的记录。

最佳答案

您可以使用计数来代替:

SELECT 
*
FROM
posts p
WHERE
(SELECT count(*) FROM following f WHERE f.from = 2 AND f.to = p.uid) > 0
OR p.uid = 2

你觉得怎么样?

关于mysql - 在 MySQL 查询中同时使用存在和简单值检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27330056/

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