此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%'
目前它返回18行,正确的6行被重复3次。我想解决这个问题的办法是使用另一个where子句,类似于:
WHERE DISTINCT feedFinishes.ID
经过研究,我尝试过:
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);
讨论后发现here
唉,这仍然返回18行。我相信答案与here
任何建议都将不胜感激。
最佳答案
你应该适用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