Mysql 从具有相同DisplayID的表中删除重复项

Mysql 从具有相同DisplayID的表中删除重复项,mysql,duplicates,heidisql,Mysql,Duplicates,Heidisql,我有一张表格如下: Name DisplayID InventoryType Itemlevel RequiredLevel Armor1 4 4 566 90 Armor2 123 30 566 90 Armor3 123 30 540 90 Armor4 123 10 540 90

我有一张表格如下:

Name   DisplayID InventoryType Itemlevel RequiredLevel  
Armor1      4           4         566      90
Armor2      123         30        566      90
Armor3      123         30        540      90
Armor4      123         10        540      90
我要做的是,从每个InventoryType中删除DisplayId副本,并保留具有最高Itemlevel的副本

HeidiSQL,MySQL

SELECT a.name, a.DisplayID, a.InventoryType, a.Itemlevel, a.RequiredLevel
FROM Item_template a
WHERE a.Itemlevel = (SELECT MAX(b.Itemlevel) FROM Item_template b where a.DisplayID = b.DisplayID)

SQL FIDLE:

假设您使用MS SQL作为数据库服务器,您可以执行以下操作。我不知道这是否是最有效的方法,但我想你会得到你期望的结果

declare @armorTable table (
    name nvarchar(10),
    displayId int,
    inventoryType int,
    itemLevel int,
    requiredLevel int
)

insert into @armorTable values ('Armor1',4,4,566,90)
insert into @armorTable values ('Armor2',123,30,566,90)
insert into @armorTable values ('Armor3',123,30,540,90)
insert into @armorTable values ('Armor4',123,10,540,90)

select
    *
from
(
    select
        *,
        (RANK() OVER (PARTITION BY displayId, inventoryType ORDER BY displayId, inventoryType, itemLevel)) Ranking
    from
        @armorTable 
) subQuery
where
    Ranking = 1
所以,这就是给你的护甲表排名。它按“displayId”和“inventoryType”对结果进行分区(分组),然后按找到的顺序对结果进行排序。因为我是在“itemLevel”上订购的,所以你会给最高的itemLevel排名1,下一个排名2,以此类推

这将产生以下结果:

name    displayId   inventoryType   itemLevel   requiredLevel   Ranking
Armor1  4           4               566         90              1
Armor4  123         10              540         90              1
Armor3  123         30              540         90              1
Armor2  123         30              566         90              2
name    displayId   inventoryType   itemLevel   requiredLevel   Ranking
Armor1  4           4               566         90              1
Armor4  123         10              540         90              1
Armor3  123         30              540         90              1
现在,您希望删除所有具有非1值的“排名”值。因为不能在where子句中直接使用RANK(),所以必须使用子查询来完成此操作

当您这样做时,您会得到以下结果:

name    displayId   inventoryType   itemLevel   requiredLevel   Ranking
Armor1  4           4               566         90              1
Armor4  123         10              540         90              1
Armor3  123         30              540         90              1
Armor2  123         30              566         90              2
name    displayId   inventoryType   itemLevel   requiredLevel   Ranking
Armor1  4           4               566         90              1
Armor4  123         10              540         90              1
Armor3  123         30              540         90              1

您可以在

上找到有关RANK()的更多信息尝试按照您的条件删除重复记录,但请注意,如果您在最高Itemlevel中有重复记录,您将在输出中看到所有记录:

DELETE Table1
  FROM Table1
     LEFT JOIN (SELECT DisplayID, InventoryType, MAX(Itemlevel) AS MaxItem
                  FROM Table1
                 GROUP BY DisplayID, InventoryType) AS Table2
     ON Table1.InventoryType = Table2.InventoryType
    AND Table1.DisplayID = Table2.DisplayID
    AND Table1.Itemlevel = Table2.MaxItem
 WHERE Table2.MaxItem IS NULL

你使用哪种关系数据库管理系统-MySQL、MS SQL Server、Oracle等等?嗨,我给的专栏只是一个例子,我有超过20000行。嗨,它删除了90%的表!不知道为什么。还有一些重复的DisplayIDsHi,谢谢,但是我是一个新手,要理解这一点,我把它粘贴在查询中,但是它给出了错误。表名是Item_template,我使用的是Heidisql,不知道是否会这样help@QuincentMaduro更新了表名代码,请立即尝试谢谢Matt!仍然有一些重复,但删除了90%,超过5k行,所以我很高兴!