0

データベースからいくつかのデータを返すためにこの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
        )
    [...]
)

そして、これは私が得ているものです(1つのトピック):

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 を追加するだけです

4

1 に答える 1

2

あなたの問題ではないかもしれませんが、この行は間違っているようです:

LEFT JOIN `topics_dislikes` AS d ON `c`.`topic_id` = `a`.`id`
于 2012-07-27T23:07:41.390 に答える