Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

limit, choice the UNION duplicate checking columns

I have two tables like the following :

parameters1

+------+-----+
| cod  | des |
+------+-----+
|  1   | aaa |
|  2   | bbb |
|  3   | ccc |

parameters2

+------+-----+
| cod  | des |
+------+-----+
|  1   | mmm |

I usually JOIN the result set of this two tables with an UNION in this way :

SELECT  cod,  des 
FROM parameters1
UNION 
SELECT  cod,  des 
FROM parameters2

and i get this result set :

+------+-----+
| cod  | des |
+------+-----+
|  1   | aaa |
|  2   | bbb |
|  3   | ccc |
|  1   | mmm |

I want to limit the UNION duplicate checking only to the cod column, so i want to avoid that the 1 cod get duplicated in the result set (when there is a record with different name and the same cod), i want to get the name from the first table (parameters1) :

+------+-----+
| cod  | des |
+------+-----+
|  1   | aaa |
|  2   | bbb |
|  3   | ccc |

UPDATE If i remove the record 1 from the parameters1 table(DELETE FROM parameters1 WHERE cod = 1) i should get this result set :

+------+-----+
| cod  | des |
+------+-----+
|  1   | mmm |   ---> FROM parameters2
|  2   | bbb |
|  3   | ccc |

Is it possible to limit the duplicate checking of a UNION to only one field or some fields ? How ?

The solution should work on a multidatabase environment (MSSQL, PostgreSQL, MySQL).

like image 675
aleroot Avatar asked Jan 24 '12 22:01

aleroot


People also ask

Does UNION query remove duplicates?

What is the difference between UNION and UNION ALL? UNION removes duplicate rows. UNION ALL does not remove duplicate rows.

Would you use UNION or UNION all if there were no duplicates?

The difference between UNION and UNION ALL is that UNION will omit duplicate records whereas UNION ALL will include duplicate records. Note: The performance of UNION ALL will typically be better than UNION , since UNION requires the server to do the additional work of removing any duplicates.

What is the difference between UNION and UNION all in SQL query?

The only difference between Union and Union All is that Union extracts the rows that are being specified in the query while Union All extracts all the rows including the duplicates (repeated values) from both the queries.


2 Answers

SELECT  cod, des 
FROM parameters1 p1
UNION ALL
SELECT  cod, des 
FROM parameters2 p2
WHERE NOT EXISTS (
    SELECT 1
    FROM parameters1 p1sub
    WHERE p1sub.cod = p2.cod
)
like image 75
Michael Fredrickson Avatar answered Oct 13 '22 19:10

Michael Fredrickson


The solution in this link should be adaptable to those environments, as it's standard SQL (you can use ISNULL if COALESCE is not available). I'm not sure what the availability of FULL OUTER JOIN is, but it should be available on all three platforms as well.

In your case, the solution will end up looking like:

SELECT  p1.cod,  ISNULL(p1.des, p2.des) AS des
FROM parameters1 p1
    FULL OUTER JOIN parameters2 p2 ON p1.cod = p2.cod

...but... MySQL apparently doesn't support FULL OUTER JOIN, so you can use another trick in that case:

SELECT  p1.cod,  ISNULL(p1.des, p2.des) AS des
FROM parameters1 p1
    LEFT JOIN parameters2 p2 ON p1.cod = p2.cod
UNION ALL
SELECT  p2.cod, p2.des
FROM parameters2 p2
    LEFT JOIN parameters1 p1 ON p1.cod = p2.cod
WHERE p1.cod IS NULL
like image 23
mwigdahl Avatar answered Oct 13 '22 19:10

mwigdahl