我正在寻找一种方法来删除不仅基于一个表而是两个表的重复项。
表1:
ID | NAME | ETC
1 | Truck |
2 | Boat |
3 | Truck |
4 | Truck |
表2
ID | REL_ID | KEY | VAL
450 | 1 | operator | Jim
451 | 2 | operator | Frank
452 | 3 | operator | Jake
453 | 4 | operator | Jim
我想以这样的方式搜索重复项:我只从表1中检索记录#4 ;表1中的标题需要重复,但表2中的相关密钥(例如rec 1和rec 3也是dupes,但它们有不同的运算符)。到目前为止,我已经尝试过两次内联表2,但我一直对嵌套的方式感到困惑。谢谢!
编辑:期望的结果:我希望运行一个只选择表1中第4行的删除的查询,因为它在名称(卡车和卡车)中都是重复的它的相关运营商(吉姆和吉姆)。如果我删除所有使用Truck作为名称的欺骗,我也会删除那些拥有不同运营商的那些。
我从表1中选择(删除)所有dupes的示例查询将是:
SELECT a.ID, a.title
FROM table_1 AS a
INNER JOIN (
SELECT title, MIN( id ) AS min_id
FROM table_1
GROUP BY title
HAVING COUNT( * ) > 1
) AS b ON b.title = a.title
AND b.min_id <> a.id
这让我从表1中获得了愚蠢,但不确定如何在表2中滚动。
答案 0 :(得分:1)
这将为您删除两个表中的值:
delete t, tt from table1 t inner join table2 tt on t.id = tt.rel_id where t.id in (select * from (select max(t1.id)
from table1 t1
inner join table2 t2
on t1.id = t2.rel_id
group by t1.name, t2.val
having count(t1.name) > 1)q)
子查询嵌套两次,因为mysql对于如何在子查询中使用要删除的表格感到挑剔。
它不会删除同一项目的多个欺骗 - 如果需要,请告诉我
这里有一个演示:http://sqlfiddle.com/#!9/0b7ea/1
此版本将删除所有欺骗,只留下ID最低的那个:
delete x, xx from table1 x inner join table2 xx on x.id = xx.rel_id where x.id in (select * from (
select t.id from table1 t
inner join
(select min(t1.id) m, t1.name, t2.val
from table1 t1
inner join table2 t2
on t1.id = t2.rel_id
group by t1.name, t2.val
having count(t1.name) > 1
) q
on t.id > q.m and t.name = q.name
inner join
table2 t2
on t.id = t2.rel_id
and t2.val = q.val) qx)
这里的