Adding DISTINCT to a UNION query

How to get a separate header. Id from this:

 SELECT Title.id, Title.title FROM titles as Title HAVING points > 0 
 UNION ALL 
 SELECT Title.id, Title.title FROM titles as Title HAVING points > 1

There is more to the request, but that should be enough.

+3
source share
3 answers

Just uninstall ALL. Some options allow you to add DISTINCTinstead ALL, to be more explicit, but redundant, that by default you always need to filter our duplicates.

MySQL - http://dev.mysql.com/doc/refman/5.0/en/union.html
MSSQL - http://msdn.microsoft.com/en-us/library/ms180026.aspx
ORACLE - https://docs.oracle.com/cd/B28359_01/server.111/b28286/queries004.htm
PostgreSQL - http://www.postgresql.org/docs/8.3/interactive/queries-union.html
.

+15

:

SELECT Title.id, Title.title FROM titles as Title HAVING points > 0 

HAVING points > 0 - HAVING points > 1?

0

ALL, .

WHERE HAVING, :

SELECT Title.id, Title.title FROM titles as Title
WHERE (1st query conditions)
   OR (2nd query conditions)

SELECT Title.id, Title.title FROM titles as Title
HAVING (1st query conditions)
    OR (2nd query conditions)
0

All Articles