2014-03-28 43 views
0

我需要的時限內取重複記錄如何在MySQL中查詢查詢結果集?

我的表是

id designation  company  pdate   location 
1  d1    c1   31-1-2014  delhi 
2  d3    c1   2-2-2014   delhi 
3  d5    c5   1-2-2014   chennai 
4  d1    c1   12-2-2014  delhi 
5  d2    c2   1-2-2014   chennai 
6  d3    c1   2-2-2014   delhi 
7  d3    c1   3-2-2014   delhi 
8  d4    c2   2-2-2014   chennai 
9  d5    c3   4-2-2014   delhi 
10 d1    c4   5-2-2014   chennai 
11 d1    c4   1-2-2014   delhi 
12 d3    c1   2-2-2014   chennai 
13 d2    c3   3-2-2014   delhi 
14 d5    c5   1-2-2014   chennai 

SELECT * FROM mytable WHERE pdate BETWEEN '1-2-2014' AND '2-2-2014'group by designation, company,location order by pdate ASC, designation ASC, company ASC, location ASC 

現在我拿來記錄

id designation  company  pdate   location 
3  d2    c2   1-2-2014   chennai 
4  d3    c1   2-2-2014   delhi 
8  d4    c2   2-2-2014   chennai 
11 d1    c4   1-2-2014   delhi 
12 d3    c1   2-2-2014   chennai 
14 d5    c5   1-2-2014   chennai 

,現在的每一行獲取我要查詢重複的(對於價值指定,公司,地點)記錄在同一張表

所以我最終的輸出將是

id designation  company  pdate   location 
2  d3    c1   2-2-2014   delhi 
5  d2    c2   1-2-2014   chennai 
6  d3    c1   2-2-2014   delhi 
7  d3    c1   3-2-2014   delhi 
8  d4    c2   2-2-2014   chennai 
10 d1    c4   5-2-2014   chennai 
12 d3    c1   2-2-2014   chennai 
14 d5    c5   1-2-2014   chennai 

所以,在我最後的結果我所有的日期2014年1月2日之間,以2014年2月2日,並與desination,公司,任何日期的位置重複值的記錄從過濾日期值也記錄

回答

0

使用一個exists條款:

select t.* 
from mytable t 
where exists (SELECT * 
       FROM mytable t2 
       WHERE pdate BETWEEN '1-2-2014' AND '2-2-2014' and 
        t2.designation = t.designation and 
        t2.company = t.company and 
        t2.location = t.location 
      ); 

你真的應該把你的時間常數YYYY-MM-DD的ISO標準格式。