在Table Plus Join中查找重复项

时间:2011-09-02 20:01:14

标签: sql sql-server tsql

我想在TableC中找到重复的手机(批次82),但手机存储在TableD

我需要在从tabled

获取手机时将tablec与自身进行比较

对于如何解决此问题的任何想法都表示赞赏?

    SELECT *
    FROM   (SELECT d.phone,
                   d.id
            FROM   tablec c
                   JOIN tabled d
                     ON c.tableid = d.id
            WHERE  c.batch = 82) d
           JOIN (SELECT d.phone,
                        d.id
                 FROM   tablec c
                        JOIN tabled d
                          ON c.tableid = d.id
                 WHERE  c.batch = 82) c
             ON d.phone = c.phone

WHERE  d.id < c.id 

2 个答案:

答案 0 :(得分:1)

SELECT d.phone, 
       MIN(d.id) MINID, MAX(d.id) MAXID , COUNT(*) COUNT
FROM   tablec c 
JOIN tabled d 
ON c.tableid = d.id 
WHERE  c.leadlistid = 81
GROUP BY d.phone
HAVING COUNT(*)> 1

答案 1 :(得分:0)

SELECT
   d.phone,
   count(c.phone)
FROM
   tableD d
   LEFT JOIN tableC c
   ON d.id = c.tableID
/* not sure what you mean by batch 82 -- maybe this: (?)
WHERE
   c.batch = 82
*/
GROUP BY
   d.phone
HAVING
   count(c.phone) > 1