0

有这四张表:

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 )

我对这个查询感到困惑,对此有什么建议吗?

谢谢

编辑 **

以下是带有示例数据的表格postsmyfavorite表格:

桌子: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
4

2 回答 2

0

好的,我自己找到了答案,我实际上是错误地使用了这部分查询:

LEFT JOIN myfavorite 
    ON ( myfavorite.member_num = posts.member_num )

它应该在哪里:

    LEFT JOIN myfavorite 
        ON ( myfavorite.recordNum = posts.num )

而且我还更改了以下WHERE条款:原始只是:

WHERE   posts.member_num = 84

编辑后的工作是:

WHERE   posts.member_num = 84 OR myfavorite.member_num = 84

所以最终的工作查询是这样的:(Query took 0.2901 sec)

SELECT  posts.title, 
    posts.num, 
    posts.status, 
    posts.category, 
    posts.content, 
    posts.member_num, 
    myfavorite.recordNum,
    myfavorite.`table`,
    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 )
    LEFT JOIN myfavorite 
        ON ( myfavorite.recordNum = posts.num )
WHERE   posts.member_num = 84 OR myfavorite.member_num = 84
GROUP   BY posts.num 
ORDER   BY posts.num 

它完全按照需要提供了两条记录。

但是我看到了一个新问题,即查询所花费的时间。我认为上述查询效率不高。

如果我将 where 子句改回WHERE posts.member_num = 84然后查询只需要0.0059 sec

我想知道这是否真的是最好和最有效的方法?

顺便说一句,感谢 @Slowcoder、@Melanie 和 @ethrbunny 的时间和建议。

于 2013-03-05T06:54:48.633 回答
0

您可以尝试以下查询吗?

SELECT  posts.title, 
    posts.num, 
    posts.status, 
    posts.category, 
    posts.content, 
    posts.member_num, 
    myfavourite.record_num,
    myfavourite.table,
    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 )
    LEFT JOIN myfavorite 
        ON ( myfavorite.member_num = posts.member_num )
WHERE   posts.member_num = 84
GROUP   BY posts.num 
ORDER   BY posts.num 
于 2013-03-05T03:44:26.447 回答