MySQL 如何在 WHERE CLAUSE 中使用 DISTINCT 来避免重复行返回

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22924049/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 20:23:38  来源:igfitidea点击:

How to use DISTINCT in WHERE CLAUSE to avoid repeated row returns

mysqlsql

提问by Smiter

The result of this SQL query should return a set of 6 rows.

此 SQL 查询的结果应返回一组 6 行。

SELECT baseFeeds.siteURL, feedFinishes.timeTaken, feedFinishes.timeSubmitted 
FROM feedFinishes 
LEFT OUTER JOIN baseFeeds 
ON feedFinishes.GUID = baseFeeds.GUID 
WHERE feedFinishes.nowDate = baseFeeds.nowDate 
AND baseFeeds.siteURL LIKE '%www.example.com%'

Currently it returns 18 rows, the correct 6 rows are being repeated 3 times. I figure a solution to this is to have another WHERE clause something similar to:

目前它返回 18 行,正确的 6 行被重复了 3 次。我想一个解决方案是有另一个 WHERE 子句类似于:

WHERE DISTINCT feedFinishes.ID

WHERE DISTINCT feedFinishes.ID

After researching this I have tried:

在研究了这个之后,我尝试过:

SELECT baseFeeds.siteURL, feedFinishes.timeTaken, feedFinishes.timeSubmitted 
FROM feedFinishes 
JOIN baseFeeds 
ON feedFinishes.GUID = baseFeeds.GUID 
WHERE baseFeeds.siteURL LIKE '%www.example.com%' AND feedFinishes.ID in
     (SELECT ID FROM feedFinishes GROUP BY ID HAVING COUNT(ID)=1);

After the discussion found here

讨论后发现here

Alas, this still returns 18 rows. I believe the answer is similar to here

唉,这仍然返回 18 行。我相信答案类似于这里

Any advice would be appreciated.

任何意见,将不胜感激。

回答by potashin

You should apply group byclause.

你应该申请group by条款。

SELECT 
baseFeeds.siteURL, feedFinishes.timeTaken, feedFinishes.timeSubmitted
FROM feedFinishes 
JOIN baseFeeds 
ON feedFinishes.GUID = baseFeeds.GUID 
WHERE baseFeeds.siteURL LIKE '%www.example.com%' 
GROUP BY
baseFeeds.siteURL, feedFinishes.timeTaken, feedFinishes.timeSubmitted