Union distinct

This is a discussion on Union distinct within the MySQL Database forums, part of the Database Forums category; Hi all, I've a query wich results is returned via a "UNION". Problem is that I'd ...


Go Back   Usenet Forums > Database Forums > MySQL Database

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 01-21-2008
Bob Bedford
 
Posts: n/a
Default Union distinct

Hi all,

I've a query wich results is returned via a "UNION". Problem is that I'd
like to return a distinct row so if the first select returns a record based
on a field, then the second must not.

like (select idx, ....from ...)union(select idx,....from ......)

so if idx is returned in the first select it must not in the second.

It is possible ?


Reply With Quote
  #2 (permalink)  
Old 01-21-2008
Luuk
 
Posts: n/a
Default Re: Union distinct


"Bob Bedford" <bob@bedford.com> schreef in bericht
news:4794987d$0$3817$5402220f@news.sunrise.ch...
> Hi all,
>
> I've a query wich results is returned via a "UNION". Problem is that I'd
> like to return a distinct row so if the first select returns a record
> based on a field, then the second must not.
>
> like (select idx, ....from ...)union(select idx,....from ......)
>
> so if idx is returned in the first select it must not in the second.
>
> It is possible ?
>


select id from nrs;
1
2
3
4
4 rows in set (0.0 sec)

select id from nrs where id<4
union
select id from nrs where id>1;
1
2
3
4
4 rows in set (0.0 sec)

what do you mean? i dont see your problem




Reply With Quote
  #3 (permalink)  
Old 01-21-2008
Bob Bedford
 
Posts: n/a
Default Re: Union distinct


"Luuk" <luuk@invalid.lan> a écrit dans le message de news:
2tie65-pgm.ln1@a62-251-88-195.adsl.xs4all.nl...
>
> "Bob Bedford" <bob@bedford.com> schreef in bericht
> news:4794987d$0$3817$5402220f@news.sunrise.ch...
>> Hi all,
>>
>> I've a query wich results is returned via a "UNION". Problem is that I'd
>> like to return a distinct row so if the first select returns a record
>> based on a field, then the second must not.
>>
>> like (select idx, ....from ...)union(select idx,....from ......)
>>
>> so if idx is returned in the first select it must not in the second.
>>
>> It is possible ?
>>

>
> select id from nrs;
> 1
> 2
> 3
> 4
> 4 rows in set (0.0 sec)
>
> select id from nrs where id<4
> union
> select id from nrs where id>1;
> 1
> 2
> 3
> 4
> 4 rows in set (0.0 sec)
>
> what do you mean? i dont see your problem

Not so simple. I've a complex query:
(select idx, firstname,lastname,address,....from person inner join status on
person.idperson = status.idperson where status.statustype = 12 and
status.statusdate < '2007-01-01')
union
(select idx, firstname,lastname,address,....from person inner join group on
person.idperson = group.idperson where group.grouptype = 27 and
group.groupdate < '2007-01-01')

as you can see I have 2 different ways to get the informations and I can't
mix the 2 in the same query (as The way I link tables is quite different)


Reply With Quote
  #4 (permalink)  
Old 01-21-2008
Rik Wasmus
 
Posts: n/a
Default Re: Union distinct

On Mon, 21 Jan 2008 14:41:14 +0100, Bob Bedford <bob@bedford.com> wrote:

>
> "Luuk" <luuk@invalid.lan> a écrit dans le message de news:
> 2tie65-pgm.ln1@a62-251-88-195.adsl.xs4all.nl...
>>
>> "Bob Bedford" <bob@bedford.com> schreef in bericht
>> news:4794987d$0$3817$5402220f@news.sunrise.ch...
>>> Hi all,
>>>
>>> I've a query wich results is returned via a "UNION". Problem is that
>>> I'd
>>> like to return a distinct row so if the first select returns a record
>>> based on a field, then the second must not.
>>>
>>> like (select idx, ....from ...)union(select idx,....from ......)
>>>
>>> so if idx is returned in the first select it must not in the second.
>>>
>>> It is possible ?
>>>

>>
>> select id from nrs;
>> 1
>> 2
>> 3
>> 4
>> 4 rows in set (0.0 sec)
>>
>> select id from nrs where id<4
>> union
>> select id from nrs where id>1;
>> 1
>> 2
>> 3
>> 4
>> 4 rows in set (0.0 sec)
>>
>> what do you mean? i dont see your problem

> Not so simple. I've a complex query:
> (select idx, firstname,lastname,address,....from person inner join
> status on
> person.idperson = status.idperson where status.statustype = 12 and
> status.statusdate < '2007-01-01')
> union
> (select idx, firstname,lastname,address,....from person inner join group
> on
> person.idperson = group.idperson where group.grouptype = 27 and
> group.groupdate < '2007-01-01')
>
> as you can see I have 2 different ways to get the informations and I
> can't
> mix the 2 in the same query (as The way I link tables is quite different)


Haven't had any coffee yet, but:
SELECT idx, firstname,lastname,address,....
FROM person
LEFT JOIN status
ON person.idperson = status.idperson
AND status.statustype = 12
AND status.statusdate < '2007-01-01'
LEFT JOIN group
ON person.idperson = group.idperson
AND group.grouptype = 27
AND group.groupdate < '2007-01-01'
WHERE group.idperson IS NOT NULL OR status.idperson IS NOT NULL

Haven't really thought the effeciency of the query through yet though.
--
Rik Wasmus
Reply With Quote
  #5 (permalink)  
Old 01-21-2008
Luuk
 
Posts: n/a
Default Re: Union distinct


"Bob Bedford" <bob@bedford.com> schreef in bericht
news:4794a104$0$3813$5402220f@news.sunrise.ch...
>
> "Luuk" <luuk@invalid.lan> a écrit dans le message de news:
> 2tie65-pgm.ln1@a62-251-88-195.adsl.xs4all.nl...
>>
>> "Bob Bedford" <bob@bedford.com> schreef in bericht
>> news:4794987d$0$3817$5402220f@news.sunrise.ch...
>>> Hi all,
>>>
>>> I've a query wich results is returned via a "UNION". Problem is that I'd
>>> like to return a distinct row so if the first select returns a record
>>> based on a field, then the second must not.
>>>
>>> like (select idx, ....from ...)union(select idx,....from ......)
>>>
>>> so if idx is returned in the first select it must not in the second.
>>>
>>> It is possible ?
>>>

>>
>> select id from nrs;
>> 1
>> 2
>> 3
>> 4
>> 4 rows in set (0.0 sec)
>>
>> select id from nrs where id<4
>> union
>> select id from nrs where id>1;
>> 1
>> 2
>> 3
>> 4
>> 4 rows in set (0.0 sec)
>>
>> what do you mean? i dont see your problem

> Not so simple. I've a complex query:
> (select idx, firstname,lastname,address,....from person inner join status
> on person.idperson = status.idperson where status.statustype = 12 and
> status.statusdate < '2007-01-01')
> union
> (select idx, firstname,lastname,address,....from person inner join group
> on person.idperson = group.idperson where group.grouptype = 27 and
> group.groupdate < '2007-01-01')
>
> as you can see I have 2 different ways to get the informations and I can't
> mix the 2 in the same query (as The way I link tables is quite different)
>


if you want the distinct results you can do

SELECT distinct * FROM (
query1
UNION
query2
}

it will still give duplicate values of your `idx` because MySQL cannot
decide for you wat to do if you have a duplicate idx.

you can do that for MySQL by adding a GROUP BY

SELECT distinct * FROM (
query1
UNION
query2
} GROUP BY idx



Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 06:31 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0