使用mysql查询获取重复记录

时间:2017-07-21 15:01:27

标签: mysql

我试图在我的表中获取重复记录并且它没有完全正常工作,

我的表格如下:

id  vehicle     dates
---------------------------
1   GKB301J     2016-04-02
2   GKB301J     2016-04-02
3   GKB301J     2016-04-03
4   GKB275F     2016-04-14
5   GKB275F     2016-04-05
6   GKB275F     2016-04-14
7   GKB275F     2016-04-13
8   GKB438F     2016-04-29

从上表中,我希望得到:

id  vehicle     dates
 1  GKB301J     2016-04-02
 2  GKB301J     2016-04-02
 4  GKB275F     2016-04-14
 6  GKB275F     2016-04-14

但我的查询效果不佳:

下面是我的代码:

SELECT j.id, j.vehicle, j.dates
FROM ISSUANCE j
INNER JOIN (
    SELECT dates, vehicle
    FROM ISSUANCE
    GROUP BY DATE_FORMAT(dates, '%Y-%m-%d')
    HAVING COUNT(id) >= 2
) i ON DATE_FORMAT(j.dates, '%Y-%m-%d') = DATE_FORMAT(i.dates, '%Y-%m-%d') AND j.vehicle = i.vehicle
WHERE j.dates BETWEEN '2016-04-01' AND '2016-04-30'

2 个答案:

答案 0 :(得分:0)

您是否尝试过使用SELECT DISTINCT - 它应该返回唯一(非重复)记录。

退房:     https://dev.mysql.com/doc/refman/5.7/en/select.html

答案 1 :(得分:0)

正如你的问题告诉你想要复制一样:

select vehicle , dates from Issuance WHERE dates BETWEEN '2016-04-01' AND '2016-04-30'
minus
(select vehicle , dates, count(*) from Issuance 
WHERE dates BETWEEN '2016-04-01' AND '2016-04-30'
group by vehicle , dates having count(*) = 1);