QUERY:MySQL的DISTINCT關鍵字不表示不同的行
mysql> SELECT DISTINCT (stories.id), stories.news_type, story_keywords.keyword
FROM (`stories`) LEFT JOIN `story_keywords` ON `stories`.`id`
=`story_keywords`.`story_id`
WHERE `image_full_url` != '' AND `order` != 0
AND `news_type` IN ('movie_review', 'movie')
AND `keyword` IN ('topnews', 'toptablet')
ORDER BY `created` DESC, `order` DESC LIMIT 5;
結果:(請注意,ID不鮮明)
+--------+--------------+-----------+
| id | news_type | keyword |
+--------+--------------+-----------+
| 100895 | movie_review | topnews |
| 100895 | movie_review | toptablet |
| 100798 | movie_review | toptablet |
| 100060 | movie_review | toptablet |
| 100060 | movie_review | topnews |
+--------+--------------+-----------+
5 rows in set (0.00 sec)
'DISTINCT'應用於整個行。所以這些是基於'關鍵字'值差異的'DISTINCT'行。如果您只想返回一個關鍵字,則必須確定要返回哪個值 – Taryn
如果在緊隨「DISTINCT」關鍵字之後刪除空格,會發生什麼情況? – cdhowie
@cdhowie:distinct不是一個函數。這只是一個語法錯誤。 –