gpt4 book ai didi

MYSQL 选择子查询 IF 存在

转载 作者:行者123 更新时间:2023-11-29 12:04:32 25 4
gpt4 key购买 nike

我有 3 个表......用户、更新和碰撞。

我想向发出 api 请求的用户返回最新订单的 feed 更新,并提供显示 feed 中每个状态所需的所有数据。我还需要包括更新是否已被发出 api 请求的用户“碰撞”(碰撞 = 喜欢 facebook 上的状态)

简而言之,这就是我正在尝试做的事情,即使此查询的 if 计数区域与正确的语法相差甚远:

SELECT b.type,b.owner,b.update_img,b.ALBUM_ID,b.last_comment,a.uid, a.first_name, a.last_name, a.gender, a.thumb_img, b.msg_id, b.message, b.created,b.POST_PRIVACY,
(if count( SELECT * FROM BUMPS WHERE b.msg_id= BUMPS.MSG_ID_FK AND BUMPS.UID_FK=$loggedin_uid)>0 THEN 1 ELSE null) as 'isBumpedBool'
FROM users AS a, updates AS b
WHERE b.uid_fk = a.uid
AND b.type<>'FRIEND_RELATIONSHIP'
AND b.created<$time
AND b.type<>'FAMILIAR_RELATIONSHIP'
AND a.college='$college'
AND b.POST_PRIVACY<>'4'
AND b.POST_PRIVACY<>'5'
AND b.created>=$tstamp
ORDER BY b.created DESC
LIMIT 100

有什么想法吗?

最佳答案

添加 LEFT JOIN 以获取所有连接到 BUMPS 的结果,如果值不存在则返回 NULL

SELECT b.type,
b.owner,
b.update_img,
b.ALBUM_ID,
b.last_comment,
a.uid,
a.first_name,
a.last_name,
a.gender,
a.thumb_img,
b.msg_id,
b.message,
b.created,
b.POST_PRIVACY,
(CASE WHEN bm.MSG_ID_FK IS NOT NULL THEN 1 ELSE NULL END) AS 'isBumpedBool'
FROM users AS a JOIN
updates AS b
ON b.uid_fk = a.uid
AND b.type<>'FRIEND_RELATIONSHIP'
AND b.created<$time
AND b.type<>'FAMILIAR_RELATIONSHIP'
AND a.college='$college'
AND b.POST_PRIVACY<>'4'
AND b.POST_PRIVACY<>'5'
AND b.created>=$tstamp
LEFT JOIN BUMPS as bm
ON b.msg_id= bm.MSG_ID_FK
AND bm.UID_FK=$loggedin_uid
ORDER BY b.created DESC LIMIT 100

关于MYSQL 选择子查询 IF 存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31769884/

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