gpt4 book ai didi

mysql - 正确的 SELECT 语句是什么?

转载 作者:可可西里 更新时间:2023-11-01 07:22:04 24 4
gpt4 key购买 nike

SELECT *
FROM notifications
INNER JOIN COMMENT
ON COMMENT.id = notifications.source_id
WHERE idblog IN (SELECT blogs_id
FROM blogs
WHERE STATUS = "active")
INNER JOIN reportmsg
ON reportmsg.msgid = notifications.source_id
WHERE uid =: uid
ORDER BY notificationid DESC
LIMIT 20;

我在这里 INNER JOINing notificationscommentreportmsg;然后使用 WHERE 过滤内容。

但我的问题是,对于第一个 INNER JOIN [即,使用 comment],在使用 comment< 加入 notifications 之前,我想将 notifications.idblogblogs.blogs_id SELECT 仅匹配那些行 blogs.status = "active".

为了更好的理解上面的代码:

ER diagram

在这里,对于 INNER JOIN,使用 comment 我只想SELECT notifications 中的那些行 idblog 匹配 blogs.blogs_id 并且 status = "active"

不需要更改带有 reportmsg 的第二个 INNER JOIN。即,它仅通过 uid 过滤。

最佳答案

从下图中可以看出,您只需像这样使用 LEFT JOIN 将其他表合并到通知表:

SELECT n.notificationid, n.uid, n.idblog, n.source_id, 
b.blogs_id, b.status,
c.id,
r.msgid
-- ... and the other columns you want
FROM notifications n
LEFT JOIN blogs b ON b.blogs_id = n.idblog AND b.STATUS = "active" AND n.uid =: uid
LEFT JOIN comment c ON c.id = n.source_id
LEFT JOIN reportmsg r ON r.msgid = n.source_id
ORDER BY n.notificationid DESC
LIMIT 20;


SQL JOINS

希望这有助于...

关于mysql - 正确的 SELECT 语句是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36218850/

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