1

I have three tables: menu_tab has columns (menu_id,menu_description) item_tab has columns (item_id,item_name,item_description,item_price) menu_has_item has columns{ (menu_tab_menu_id ---> which is foreign key to menu_id (pk in menu_tab)), item_tab_item_id --- which is foreign key to item_id (pk in item_tab))4

there will be 2 kinds of duplicates which will be encountered 1)Item duplicate in the same menu_description 2)Item duplicate in a different menu description

Example: Two Chicken Sandwiches in the lunch menu. One Chicken Sandwich in Lunch and another in Dinner menu _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 replace by 3
2............................6
3............................7
3............................8 replace by 1
3............................9

How do I update my menu_has_item with the replaced values after removing the duplicates?

4

3 回答 3

1
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;
于 2013-02-23T06:01:46.393 回答
0

首先,您需要用新值替换 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,我错误地认为你关于Oracle,对不起。但我想在 MySQL 中存在合并语句祝你好运

于 2013-02-21T18:24:29.697 回答
0

我为我的表 Rout(RoutID,SourceCityID,DestCityID) 和 Form(FormID,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

现在删除 Rout 中不在表格中的行

DELETE FROM Rout WHERE RoutID NOT IN(SELECT DISTINCT RoutID FROM Form)
于 2014-09-17T22:54:14.037 回答