有这四张表:
posts
posts_replies
likes
myfavorite
我正在使用以下查询从前 3 个表中返回数据。但是第四张表(我最喜欢的),我希望它也包含在其中并加载用户选择为收藏夹的帖子。
SELECT posts.title,
posts.num,
posts.status,
posts.category,
posts.content,
posts.member_num,
COUNT( posts_replies.post_num ) AS count,
COUNT( likes.comment_num ) AS likes_count
FROM posts
LEFT JOIN posts_replies
ON ( posts_replies.post_num = posts.num )
LEFT JOIN likes
ON ( likes.comment_num = posts_replies.num )
WHERE posts.member_num = '84'
GROUP BY posts.num
ORDER BY posts.num
myfavorite 表具有以下字段:
id (primary key)
member_num
recordNum
table ('posts' OR 'members')
我尝试将以下左连接添加到上述查询中以从 myfavorite 表中提取记录,但它不起作用:
LEFT JOIN myfavorite
ON ( myfavorite.recordNum = posts.num )
我对这个查询感到困惑,对此有什么建议吗?
谢谢
编辑 **
以下是带有示例数据的表格posts
和myfavorite
表格:
桌子:myfavorite
id | member_num | recordNum | table
72 | 84 | 74 | blyrbs
桌子:posts
title | num | status | category | content | member_num
Testing Post| 117 | 1 | 0 | testing... | 84
My Fav Post | 107 | 1 | 0 | this is the fav post... | 22