MySQL Select / Join仅返回第一行

时间:2012-07-27 23:05:47

标签: mysql select join left-join

我有这个MySQL查询从我的数据库返回一些数据,但由于某种原因,它只返回第一行,我可能做错了什么?

SELECT `a`.`id` AS topic_id, `a`.`text` AS topic_text, 
 `a`.`date` AS topic_date, `b`.`name` AS author_name, 
 `b`.`picture` AS author_picture, `b`.`facebook_id` AS author_facebook_id, 
SUM(CASE WHEN c.topic_id IS NOT NULL THEN 1 ELSE 0 END) AS topic_likes_number, 
SUM(CASE WHEN c.topic_id IS NOT NULL AND c.author_id = 1 THEN 1 ELSE 0 END) AS topic_liked, 
SUM(CASE WHEN d.topic_id IS NOT NULL THEN 1 ELSE 0 END) AS topic_dislikes_number, 
SUM(CASE WHEN d.topic_id IS NOT NULL AND c.author_id = 1 THEN 1 ELSE 0 END) AS topic_disliked, 
SUM(CASE WHEN e.topic_id IS NOT NULL THEN 1 ELSE 0 END) AS topic_comments_number
    FROM (`topics` AS a)
    INNER JOIN `users` AS b ON `b`.`id` = `a`.`author_id`
    LEFT JOIN `topics_likes` AS c ON `c`.`topic_id` = `a`.`id`
    LEFT JOIN `topics_dislikes` AS d ON `c`.`topic_id` = `a`.`id`
    LEFT JOIN `comments` AS e ON `e`.`topic_id` = `a`.`id`
    LIMIT 50

这就是我期待的(多个主题):

Array
(
    [0] => Array
        (
            [topic_id] => 1
            [topic_text] => Donec id elit non mi porta gravida at eget metus. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus. Etiam porta sem malesuada magna mollis euismod. Donec sed odio dui.
            [topic_date] => 0000-00-00 00:00:00
            [author_name] => Claudius Ibn
            [author_picture] => 356a192b7913b04c54574d18c28d46e6395428ab.jpg
            [author_facebook_id] => 100003268692398
            [topic_likes_number] => 1
            [topic_liked] => 1
            [topic_dislikes_number] => 0
            [topic_disliked] => 0
            [topic_comments_number] => 0
        )
    [1] => Array
        (
            [topic_id] => 2
            [topic_text] => Donec id elit non mi porta gravida at eget metus. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus. Etiam porta sem malesuada magna mollis euismod. Donec sed odio dui.
            [topic_date] => 0000-00-00 00:00:00
            [author_name] => Claudius Ibn
            [author_picture] => 356a192b7913b04c54574d18c28d46e6395428ab.jpg
            [author_facebook_id] => 100003268692398
            [topic_likes_number] => 1
            [topic_liked] => 1
            [topic_dislikes_number] => 0
            [topic_disliked] => 0
            [topic_comments_number] => 0
        )
    [2] => Array
        (
            [topic_id] => 3
            [topic_text] => Donec id elit non mi porta gravida at eget metus. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus. Etiam porta sem malesuada magna mollis euismod. Donec sed odio dui.
            [topic_date] => 0000-00-00 00:00:00
            [author_name] => Claudius Ibn
            [author_picture] => 356a192b7913b04c54574d18c28d46e6395428ab.jpg
            [author_facebook_id] => 100003268692398
            [topic_likes_number] => 1
            [topic_liked] => 1
            [topic_dislikes_number] => 0
            [topic_disliked] => 0
            [topic_comments_number] => 0
        )
    [...]
)

这就是我得到的(一个主题):

Array
(
    [0] => Array
        (
            [topic_id] => 1
            [topic_text] => Donec id elit non mi porta gravida at eget metus. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus. Etiam porta sem malesuada magna mollis euismod. Donec sed odio dui.
            [topic_date] => 0000-00-00 00:00:00
            [author_name] => Claudius Ibn
            [author_picture] => 356a192b7913b04c54574d18c28d46e6395428ab.jpg
            [author_facebook_id] => 100003268692398
            [topic_likes_number] => 1
            [topic_liked] => 1
            [topic_dislikes_number] => 0
            [topic_disliked] => 0
            [topic_comments_number] => 0
        )

)

解决方案: 只需添加一个group_by a.id

1 个答案:

答案 0 :(得分:2)

这条线似乎不对,即使它可能不是你的问题:

LEFT JOIN `topics_dislikes` AS d ON `c`.`topic_id` = `a`.`id`