如何使用mysql中的join从表中删除重复项

时间:2018-01-19 06:20:27

标签: mysql duplicates left-join self-join

select * 
from
(
  SELECT  id, imei1, status
  FROM `owarranty_imei` mto 
  WHERE EXISTS
  (
    SELECT 1 
    FROM `owarranty_imei` mti 
    WHERE mto.imei1=mti.imei1 
    LIMIT 1, 1
  )
) t1 
left join `owarranty_warranty_activations` as t2 on t1.id=t2.imei_id
where t2.id is null 
limit 100

这是我的查询。在owarranty_imei有超过10万条记录。我想从imei表中获取重复项,owarranty_imei不在owarranty_warranty_activation表中。此查询适用于少数记录,但是当我运行超过1000000条记录时,它无效

1 个答案:

答案 0 :(得分:0)

  SELECT  
       mto.id, 
       mto.imei1, 
       mto.status
  FROM 
      `owarranty_imei` mto 
          INNER JOIN 
      `owarranty_imei` mti ON mto.imei1=mti.imei1 
          LEFT JOIN 
      `owarranty_warranty_activations` as t2 ON mto.id=t2.imei_id
  GROUP BY mto.id
  HAVING COUNT(t2.id)=0