我有三个表:menu_tab有列(menu_id,menu_description)item_tab有列(ITEM_ID,ITEM_NAME,ITEM_DESCRIPTION,ITEM_PRICE)menu_has_item有列{(menu_tab_menu_id --->这是外键menu_id(PK中menu_tab)),item_tab_item_id - - 这是外键ITEM_ID(在item_tab PK))4
会有2种重复的将要遇到的1)物品在同一menu_description 2)项目复制在不同的菜单说明重复
例如:两个香鸡的午餐菜单。 在午餐的一个鸡肉三明治,另一个在晚宴菜单_description
menu_tab
menu_id menu_description
1 lunch
2 dinner
3 Specials
item_tab
item_id item_description
1 b
2 d
3 g
4 x
5 g delete g
6 d
7 e
8 b delete b
9 x
menu_has_tab
menu_tab_menu_id item_tab_item_id
1 ............................ 1
1 ............................ 2
1 ............................ 3
1 ............................ 4
2 ............................ 5由3取代
2 ............................ 6
3 ............................ 7
3 ............................ 8由1代替
3 ............................ 9
我怎样去除重复之后被替换的值更新我的menu_has_item?
begin
for x in (
-- find duplicate items
select *
from (select rowid row_id,
item_id,
item_description,
row_number() over(partition by item_description order by
item_description) row_no
from item_tab)
where row_no > 1) loop
-- replaceing duplicate Items
update menu_has_item
set menu_has_item.item_tab_item_id =
( select item_id
from (select item_id,
row_number() over(partition by item_description order by
item_description) row_no
from item_tab where
item_tab.item_description = x.item_description)
where row_no = 1)
where menu_has_item .item_tab_item_id = x.item_id;
-- deleting duplicate items
delete item_tab where rowid = x.row_id;
end loop;
-- commit;
end;
首先你需要更换你的重复与新价值menu_tab
merge into menu_tab dest
using (select *
from (select item_id, min(item_id) over(partition by item_description) as new_item_id from item_tab)
where item_id != new_item_id) src
on (dest.item_tab_item_id = src.item_id)
when matched then
update set dest.item_tab_item_id = new_item_id;
之后,你需要删除从项目表副本,你可以找到脚本有http://sprogram.com.ua/en/articles/oracle-delete-duplicate-record
UPS您标记问题如PLSQL和我mistakelly认为你有关甲骨文公司的,对不起。 但我supose在MySQL存在合并声明好运
我这样做是为了我的表溃败(RoutID,SourceCityID,DestCityID)和表格(FormID,RoutID,...)我删除重复击溃从表溃败和形式表更新RoutID
首先得到由列分组重复的行,美想要比较重复
(SELECT * FROM
Rout,
(SELECT MIN(RoutID) MinRoutID
FROM Rout,
(SELECT SourceCityID,DestCityID
FROM Rout
GROUP BY SourceCityID,DestCityID
HAVING count(*) > 1) AS Duplicates
WHERE Rout.SourceCityID=Duplicates.SourceCityID AND Rout.DestCityID=Duplicates.DestCityID
GROUP BY Rout.SourceCityID,Rout.DestCityID)AS MRCols
WHERE RoutID=MinRoutID)AS DuplicateGroup
然后得到不进行分组,并与列的所有重复的行会重复进行比较
(SELECT RoutID,Rout.SourceCityID,Rout.DestCityID FROM Rout,
(SELECT SourceCityID,DestCityID
FROM Rout
GROUP BY SourceCityID,DestCityID
HAVING count(*) > 1)AS Duplicates
WHERE Rout.SourceCityID=Duplicates.SourceCityID AND Rout.DestCityID=Duplicates.DestCityID)AS DuplicateDetail
然后更新表单TBL象下面这样:
UPDATE Form SET RoutID=DuplicateGroup.RoutID
FROM
Form,
(SELECT * FROM
Rout,
(SELECT MAX(RoutID) MinRoutID
FROM Rout,
(SELECT SourceCityID,DestCityID
FROM Rout
GROUP BY SourceCityID,DestCityID
HAVING count(*) > 1) AS Duplicates
WHERE Rout.SourceCityID=Duplicates.SourceCityID AND Rout.DestCityID=Duplicates.DestCityID
GROUP BY Rout.SourceCityID,Rout.DestCityID)AS MRCols
WHERE RoutID=MinRoutID)AS DuplicateGroup
,
(SELECT RoutID,Rout.SourceCityID,Rout.DestCityID FROM Rout,
(SELECT SourceCityID,DestCityID
FROM Rout
GROUP BY SourceCityID,DestCityID
HAVING count(*) > 1)AS Duplicates
WHERE Rout.SourceCityID=Duplicates.SourceCityID AND Rout.DestCityID=Duplicates.DestCityID)AS DuplicateDetail
WHERE
Form.RoutID=DuplicateDetail.RoutID AND
DuplicateGroup.SourceCityID=DuplicateDetail.SourceCityID
AND DuplicateGroup.DestCityID=DuplicateDetail.DestCityID
现在删除溃败以表格形式的arent行
DELETE FROM Rout WHERE RoutID NOT IN(SELECT DISTINCT RoutID FROM Form)