SQL:INNER JOIN SELECT使整个查询不返回任何内容

时间:2016-06-30 19:55:47

标签: mysql sql join

有3个表,名为account_has_account1, account_has_photos, photos_has_message_photosaccount_has_account1account_id, account_id1, status, type_id,此表负责将帐户存储到其他帐户

account_has_photos存储有关一个帐户上传过的所有照片的信息,其列为photos_id, account_id, type_id,根据likes

还会存储type_id

photos_has_message_photos存储发布到照片的所有邮件,其中包含来自photosmessage_photos的关系表

我需要从account_has_photos获取所有喜欢的计数,其中type_id = 1指向表like中的type

我做了这个SQL

SELECT account_has_photos.photos_id as id, "photos" as type, account_has_photos.update_at, account_has_photos.account_id, posts.total as total_messages, likes.total as total_likes
FROM account_has_account1 
    INNER JOIN account_has_photos 
        ON (account_has_photos.account_id = account_has_account1.account_id1 AND account_has_photos.type_id = 17)
    INNER JOIN (
        SELECT photos_has_message_photos.photos_id, count(*) as total
        FROM photos_has_message_photos
        GROUP BY photos_has_message_photos.photos_id
    ) posts
        ON(posts.photos_id = account_has_photos.photos_id)
    INNER JOIN (
        SELECT account_has_photos.photos_id, COUNT(account_has_photos.photos_id) as total
        FROM account_has_photos
        WHERE account_has_photos.type_id = 1
    ) likes
        ON (likes.photos_id = account_has_photos.photos_id)
WHERE account_has_account1.account_id = 7 AND account_has_account1.`status` = "Active"

第一个INNER JOIN account_has_account1用于显示一个帐户所关注的所有帐户,第二个INNER JOIN photos_has_message_photos仅用于获取帐户照片中所有已发布消息的计数。

此时一切正常,但是当我再次插入第三个INNER JOIN account_has_photos时,查询结果现在为0,第三个JOIN的目的是获取照片的数量它存储在account_has_photos type_id = 1

其余用于设置搜索的一般条件。

此问题仅在此查询中发生

INNER JOIN (
        SELECT account_has_photos.photos_id, COUNT(account_has_photos.photos_id) as total
        FROM account_has_photos
    ) likes
        ON (likes.photos_id = account_has_photos.photos_id)

可能是在任何照片上都找不到喜欢的东西,我已经做了单独搜索的测试,正如我所说,没有像任何照片一样,我没有添加任何记录,因为我想要它在计数时说0,因为它会显示很多

2 个答案:

答案 0 :(得分:1)

您的上一个子查询缺少GROUP BY。试试这个:

INNER JOIN (
    SELECT account_has_photos.photos_id, COUNT(account_has_photos.photos_id) as total
    FROM account_has_photos
    GROUP BY account_has_photos.photos_id
) likes
    ON likes.photos_id = account_has_photos.photos_id

如果您想要没有喜欢的行,您可能还想将INNER JOIN替换为LEFT OUTER JOIN

答案 1 :(得分:1)

这里有一种不同的方式来编写你的查询,它应该得到相同的结果。

SELECT
    account_has_photos.photos_id as id
    ,"photos" as type
    ,account_has_photos.update_at
    ,account_has_photos.account_id
    ,COUNT(photos_has_messages.photos_id) as total_messages
    ,COUNT(DISTINCT likes.photos_id) as total_likes
FROM
    account_has_account1
    INNER JOIN account_has_photos
    ON account_has_photos.account_id = account_has_account1.account_id1
    AND account_has_photos.type_id = 17
    LEFT JOIN photos_has_message_photos
    ON photos_has_message_photos.photos_id = account_has_photos.photos_id
    LEFT JOIN account_has_photos likes
    ON likes.photo_id = account_has_photos.photo_id
    AND likes.type_id = 1
    WHERE account_has_account1.account_id = 7 AND account_has_account1.`status` = "Active"
GROUP BY
    account_has_photos.photos_id
    ,"photos"
    ,account_has_photos.update_at
    ,account_has_photos.account_id

我建议改变:

,COUNT(photos_has_messages.photos_id) as total_messages

,COUNT(DISTINCT photos_has_messages.WhateverTablesUniqueIdIs) as total_messages

也是这一行

,COUNT(DISTINCT likes.photos_id) as total_likes

总是会给你1.所以如果喜欢重复photo_id,那么你也想计算account_has_photos唯一标识符是什么....