简体   繁体   English

先进的SQL查询设计说明(跨两个表,多个域重复,可以排除基于一个字段)

[英]Advanced SQL Query Design Help (Duplicates across two tables, multiple fields, possible exclusions based on one field)

I have two tables, named: 我有两个表,名为:

1) item 1)项目

2) bankitem 2)bankitem

Item has the following fields: 物料具有以下字段:

ID, CharID, Name, ItemID, Count, Type, ID1, ID2, ID3, Color, Effect1, Effect2, Effect3, LifeSpan, Attribute, Equip, X, Y ID,CharID,名称,ItemID,计数,类型,ID1,ID2,ID3,颜色,Effect1,Effect2,Effect3,LifeSpan,属性,Equip,X,Y

Bankitem has the following fields: Bankitem具有以下字段:

ID, CharID, Name, ItemID, Count, Type, ID1, ID2, ID3, Color, Effect1, Effect2, Effect3, LifeSpan, Attribute ID,CharID,名称,ItemID,计数,类型,ID1,ID2,ID3,颜色,Effect1,Effect2,Effect3,LifeSpan,属性

Objective: 目的:

I would like to pull an individual listing (ie, not just a count) from across these two tables that have the following fields in common: Name, ItemID, ID1, ID2, and ID3 so that I can scan the listing and investigate any duplicate entries, prior to deleting one of them. 我想从这两个具有以下共同字段的表中拉出一个单独的列表(即,不只是一个计数):名称,ItemID,ID1,ID2和ID3,以便我可以扫描列表并调查任何重复项项,然后删除其中之一。 Every other field could potentially be different based on the nature of how the database is updated. 根据数据库更新方式的性质,其他每个字段都可能有所不同。

Also, sometimes there may certain records that are okay to have duplicates (again, based on the nature of how the database is used). 另外,有时某些记录可以重复(同样,取决于数据库的使用方式)。 Is there a way to build into the script to exclude duplicates (based on the above fields) if the field "Name" equals one of the exclusions I could set up? 如果字段“名称”等于我可以设置的排除项之一,是否有一种方法可以内置到脚本中以排除重复项(基于上述字段)?

Any help would be greatly appreciated. 任何帮助将不胜感激。 I am relatively new to SQL queries - I know what I want to do, but am really lost on how to get to the next step. 我对SQL查询还是比较陌生的-我知道我想做什么,但实际上对如何进行下一步很迷失。

Thanks. 谢谢。

SELECT Name, ItemID, ID1, ID2,ID3 FROM item
UNION ALL
SELECT Name, ItemID, ID1, ID2,ID3 FROM bankitem

Or use UNION if you don't want duplicates.If you dont want duplicates in the table just use an unique index on that column. 如果不想重复,也可以使用UNION。如果不想重复使用表,只需在该列上使用唯一索引。

SELECT * FROM(SELECT Name, ItemID, ID1, ID2, ID3, count(*) no_of_records FROM item 
UNION 
SELECT Name, ItemID, ID1, ID2, ID3, count(*) no_of_records FROM bankitem 
GROUP BY Name, ItemID, ID1, ID2, ID3 HAVING count(*) > 1)as x 
WHERE x.Name != 'RedPotion'

Or 要么

WHERE x.Name NOT IN('blah1','blah2')

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

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