简体   繁体   English

SQL查询设计帮助:在多个表和排除项中查找重复项

[英]SQL Query Design Help: Finding duplicates in multiple tables & exclusions

I am trying to design a query that picks up duplicate records from across two tables based on a few fields. 我正在尝试设计一个查询,该查询根据几个字段从两个表中选取重复的记录。 I've set up what I've done so far here: http://sqlfiddle.com/#!2/e36c8d/1/0 我已经在这里设置了到目前为止所做的事情: http : //sqlfiddle.com/#!2/e36c8d/1/0

Some context: 一些背景:

1) These two tables identified below house 'items' for a game that players play. 1)下面两个表格列出了玩家玩游戏的“项目”。 Items come from either drops from monsters/player trades. 物品来自怪物/玩家交易中的任一掉落。

2) Players in the game are identified by their CharID 2)游戏中的玩家通过其CharID进行标识

3) Items are primarily defined by the following unique fields: Name, ItemID, ID1, ID2, ID3. 3)项目主要由以下唯一字段定义:名称,项目ID,ID1,ID2,ID3。

4) There are other attributes for each item, but these can be changed depending on what the player does (eg changes colour of item; moves it position, upgrades it). 4)每个项目都有其他属性,但是可以根据玩家的行为进行更改(例如,更改项目的颜色;移动项目的位置,对其进行升级)。

5) There are two tables where items are stored: ITEM and BANKITEM. 5)有两个存储项目的表:ITEM和BANKITEM。 Item = bag; 项目=包; Bankitem = warehouse. Bankitem =仓库。

There are occasions where players can (accidentally, or intentionally) duplicate individual items and then trade those items to other players or use on another one of their characters. 在某些情况下,玩家可以(偶然或有意)复制单个物品,然后将这些物品交易给其他玩家或在他们的另一个角色上使用。

Needs (Really important to meet all 3 needs): 需求(对满足所有三个需求非常重要):

1) I need the query to scan both tables simultaneously to identify items that have duplicate Name, ItemID, ID1, ID2, ID3 that I can then investigate further (and delete one of the duplicates). 1)我需要查询以同时扫描两个表,以识别具有重复的名称,ItemID,ID1,ID2,ID3的项目,然后我可以对其进行进一步调查(并删除其中一个重复项目)。

2) I need the query to exclude certain items based on Name (eg, RedPotions all have the same Name, ItemID, ID1, ID2, and ID3. These are common items and duplicates are fine...I don't need them included in the listing as they are not rare/high value items). 2)我需要查询以基于名称排除某些项目(例如,RedPotions都具有相同的名称,ItemID,ID1,ID2和ID3。这些是常见项目,重复项可以...我不需要包含它们在清单中,因为它们不是稀有/高价值的物品)。

3) I need the query to exclude CharID that are NULL (This is where I am having real difficulty because CharID is not part of my SELECT statement, and it can't be because it is entirely possible for two different CharIDs to have the same duplicate rare item). 3)我需要查询以排除NULL的CharID (这是我真正遇到困难的地方,因为CharID不是我的SELECT语句的一部分,并且不可能,因为两个不同的CharID完全有可能具有相同的含义重复的稀有物品)。

SQL FIDDLE: SQL FIDDLE:

If the query was working properly, the results should show: 如果查询工作正常,结果应显示:

  • 2 duplicate ChainHose(M) with the following common fields: ChainHose(M), 100, 17089, 22452, -12225 2个重复的ChainHose(M),具有以下公共字段:ChainHose(M),100、17089、22452,-12225

  • The ChainHose(M) with the CharID of '0' would be excluded from the listing. CharID为'0'的ChainHose(M)将从列表中排除。 Note the actual field in the table is NULL and not '0'. 请注意,表中的实际字段为NULL,而不是'0'。 (I am new to SQL/SQL Fiddle and wasn't sure how to make it 'NULL' in SQL Fiddle for example purposes. (我是SQL / SQL Fiddle的新手,不确定出于示例目的如何在SQL Fiddle中将其设置为“ NULL”。

  • 2 duplicate Hauberk(W) with the following common fields: Hauberk(W), 200, 12369, 15252, 95682. It doesn't matter that the colour and lifespan are different (player with the duplicate could have dyed the armour after obtaining the duplicated item. The lifespan could have been reduced through use). 2个重复的Hauberk(W),具有以下公共字段:Hauberk(W),200、12369、15252、95682。颜色和寿命不同也没关系(重复的玩家可以在获得重复的商品。使用寿命可能会因使用而减少)。

Does anyone have any advice? 有人有建议吗? I've previously asked a similar question without resolve (see Advanced SQL Query Design Help (Duplicates across two tables, multiple fields, possible exclusions based on one field) ). 我以前曾问过类似的问题,但没有解决(请参阅高级SQL查询设计帮助(两个表中的重复项,多个字段,基于一个字段的可能排除项) )。 The person recommended I be more specific and to use SQL Fiddle, so I have done so in hopes I can get this working properly. 该人员建议我更加具体,并使用SQL Fiddle,因此我这样做是希望我可以使其正常工作。

Thanks in advance. 提前致谢。

Is this what you need? 这是您需要的吗?

SQL Fiddle SQL小提琴

SELECT *
from bankitem
where exists(
  SELECT Name, ItemID, ID1, ID2, ID3
  from item
  where bankitem.Name = item.Name
  and bankitem.ItemID = item.ItemID
  and bankitem.ID1 = item.ID1
  and bankitem.ID2 = item.ID2
  and bankitem.ID3 = item.ID3
)
and name not in('RedPotion')
and charid <> 0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM