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
和notifications
过滤内容。但我的问题是,对于第一个
comment
[即,使用reportmsg
],在将WHERE
与INNER JOIN
连接之前,我希望将comment
与notifications
匹配,并且仅将comment
的行与notifications.idblog
匹配。为了更好地理解上述代码:
在这里,对于
blogs.blogs_id
,使用SELECT
我只希望blogs.status = "active"
中INNER JOIN
匹配comment
且具有SELECT
的行。带
notifications
的第二个idblog
不需要更改。也就是说,它只过滤blogs.blogs_id
。 最佳答案
如下图所示,您只需使用类似的LEFT JOIN
将其他表合并到notifications表:
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;
希望这有帮助…