I’m desperate with this query. I have two tables table1 and table2, tables are identical but they have different data. I’m trying to remove duplicities by columns code and manufacturer. To do that I need in final result ID from table1 ID from table2 and also columns code and manufacturer
SELECT * FROM (
SELECT id,code,manufacturer FROM table1 WHERE manufacturer = 1
UNION SELECT id,code,manufacturer FROM table2 WHERE manufacturer = 1
) AS t GROUP BY code HAVING COUNT(*) > 1
But in result i got only values from table1. It’s OK but I just need to get there id from table2 too. Please can anyone give me some tips how to do this ?
You have two basic problems:
Problem 1:
You are using
UNIONwhen you should be usingUNION ALL, becauseUNIONremoves duplicates!Problem 2:
This isn’t the right way to go about the problem. You should be using a simple join, not a union.
Try this:
Your use of the
WHEREclause is a little odd – consider removing it to get all duplicates from all manufacturers.