[thelist] MySQL Query problem: unique vals from two fields

Pringle, Ron RPringle at aurora-il.org
Wed Nov 22 12:54:18 CST 2006


To close this out, turns out you don't need to use DISTINCT in the
query. The default behavior of UNION in MySQL is to remove duplicate
rows anyhow. So the final query now looks like:

select year(exEndDate) as exhibitYear
from tbl_exhibitDates
where exEndDate < current_date()
union
select year(exStartDate)
from tbl_exhibitdates
where exStartDate < current_date()

Thanks again Matt.


Ron


Matt wrote:

Fresh out of blunt objects, but maybe I can help with the query.

select distinct year(exEndDate) as exhibitYear
from tbl_exhibitDates
where exEndDate < current_date()
union
select distinct year(exStartDate)
from tbl_exhibitdates
where exStartDate < current_date()

This *should* return only distinct years, because it is union and not
union all. However, give it a try in your DB of choice and if it is
duping years that exist in both sets, we will have to filter those out
in the where clause of the second query.

HTH,

-- 
Matt Warden
Cleveland, OH, USA
http://mattwarden.com


More information about the thelist mailing list