简体   繁体   中英

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

I have two tables, named:

1) item

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

Bankitem has the following fields:

ID, CharID, Name, ItemID, Count, Type, ID1, ID2, ID3, Color, Effect1, Effect2, Effect3, LifeSpan, Attribute

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. 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.

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.

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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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