GROUP BY未返回正确的结果

时间:2016-04-29 07:40:13

标签: mysql

此查询

SELECT releases.id,releases.label_no_country,releases.date 
FROM releases 
INNER JOIN labels_user_has_recd luhr 
ON luhr.label=releases.label_no_country 
WHERE luhr.user='Si Quick' 
GROUP BY releases.id 
HAVING datediff(now(),releases.date) < 90 
ORDER BY releases.date DESC 
LIMIT 0,10

给出

id  label_no_country    date
605639  Church  2016-04-28
595303  Uncanny Valley  2016-04-21
605806  Dekmantel   2016-04-21
607276  Uncanny Valley  2016-04-21
604280  Delsin  2016-04-21
598653  Another Picture     2016-04-21
605016  Skylevel    2016-04-20
606342  Retreat     2016-04-14
599363  Frank Music     2016-04-12
599320  Spazio Disponibile  2016-04-08

我希望不会在label_no_count列中返回任何重复项(即不是Uncanny Valley两次。

我尝试在以下查询中使用GROUP BY

SELECT releases.id,releases.label_no_country,releases.date 
FROM releases 
INNER JOIN labels_user_has_recd luhr 
ON luhr.label=releases.label_no_country 
WHERE luhr.user='Si Quick' 
GROUP BY releases.label_no_country 
HAVING datediff(now(),releases.date) < 90 
ORDER BY releases.date DESC 
LIMIT 0,10

但这只返回一个结果,而不是删除重复项

id  label_no_country    date
599320  Spazio Disponibile  2016-04-08

1 个答案:

答案 0 :(得分:1)

HAVING移至WHERE条件,如

WHERE datediff(now(),releases.date) < 90 

您的查询应该是

SELECT releases.id,releases.label_no_country,releases.date 
FROM releases 
INNER JOIN labels_user_has_recd luhr 
ON luhr.label=releases.label_no_country 
WHERE luhr.user='Si Quick'
AND datediff(now(),releases.date) < 90  
GROUP BY releases.label_no_country 
ORDER BY releases.date DESC 
LIMIT 0,10