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 JOIN
ing notifications
和 comment
和 reportmsg
;然后使用 WHERE
过滤内容。
但我的问题是,对于第一个 INNER JOIN
[即,使用 comment
],在使用 comment< 加入
,我想将 notifications
之前notifications.idblog
与 blogs.blogs_id
和 SELECT
仅匹配那些行 blogs.status = "active"
.
为了更好的理解上面的代码:
在这里,对于 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;
希望这有助于...
关于mysql - 正确的 SELECT 语句是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36218850/