Solved

How to get a union query to return distinct rows

Posted on 2011-02-15
3
618 Views
Last Modified: 2012-06-27
Hi

Is there a way to get a union query to only return a distinct set of rows if there is overlap in the rows returned by the multiple select statements?

thanks
0
Comment
Question by:andieje
3 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 34900286
UNION does already a distinct, implicitly (unlike UNION ALL which does not perform a distinct).

however, UNION and DISTINCT both do the distinct on the returned column sets, not just on a "primary key" field.

so, you need to clarify what exactly you consider "distinct", and apply some sql techniques accordingly.
consider reading this article to find explanations and solution: http://www.experts-exchange.com/A_3203.html
0
 

Author Closing Comment

by:andieje
ID: 34900469
thanks
0
 
LVL 51

Expert Comment

by:Huseyin KAHRAMAN
ID: 34900487
check these

(select 1 union select 2)
1
2

(select 1 union select 3 union select 1 union select 2)
1
2
3

(select 1 union select 2)
union
(select 1 union select 3 union select 1 union select 2)
1
2
3
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
MySqlDump not dumping triggers 1 53
mysql date time 14 43
mysql db 3 82
Uploading a CSV Data Import via PHP & MySql 3 58
More Fun with XML and MySQL – Parsing Delimited String with a Single SQL Statement Are you ready for another of my SQL tidbits?  Hopefully so, as in this adventure, I will be covering a topic that comes up a lot which is parsing a comma (or other…
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question