Unix Technical Forum

help with DISTINCT keyword

This is a discussion on help with DISTINCT keyword within the MySQL forums, part of the Database Server Software category; --> Hi, I'm trying to select only unique values from 3 columns (filter1,filter2,filter3) and return them in their original columns. ...


Go Back   Unix Technical Forum > Database Server Software > MySQL

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 11:32 AM
Ciaran
 
Posts: n/a
Default help with DISTINCT keyword

Hi,
I'm trying to select only unique values from 3 columns
(filter1,filter2,filter3) and return them in their original columns.
I've got this so far but the UNION simply lists all results under the
filter1 column. How can I correct this please?

SELECT DISTINCT filter1 AS filter1
FROM `home_index`
UNION
SELECT DISTINCT filter2 AS filter2
FROM `home_index`
UNION
SELECT DISTINCT filter3 AS filter3
FROM `home_index`

Thanks for any help!
Ciarán
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 11:32 AM
PleegWat
 
Posts: n/a
Default Re: help with DISTINCT keyword

On Sun, 10 Feb 2008 06:18:44 -0800, Ciaran wrote:

> Hi,
> I'm trying to select only unique values from 3 columns
> (filter1,filter2,filter3) and return them in their original columns.
> I've got this so far but the UNION simply lists all results under the
> filter1 column. How can I correct this please?
>
> SELECT DISTINCT filter1 AS filter1
> FROM `home_index`
> UNION
> SELECT DISTINCT filter2 AS filter2
> FROM `home_index`
> UNION
> SELECT DISTINCT filter3 AS filter3
> FROM `home_index`
>
> Thanks for any help!
> Ciarán


Are you looking for this?

SELECT DISTINCT filter1 AS filter1, '' AS filter2, '' AS filter3
FROM home_index
UNION
SELECT DISTINCT '' AS filter1, filter2 AS filter2, '' AS filter3
FROM home_index
UNION
SELECT DISTINCT '' AS filter1, '' AS filter2, filter3 AS filter3
FROM home_index

--
PleegWat
Remove caps to reply
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-28-2008, 11:32 AM
Ciaran
 
Posts: n/a
Default Re: help with DISTINCT keyword

On Feb 10, 2:41 pm, PleegWat
<pleegwat.REM...@CAPS.leegwater-68.nl.INVALID> wrote:
> On Sun, 10 Feb 2008 06:18:44 -0800, Ciaran wrote:
> > Hi,
> > I'm trying to select only unique values from 3 columns
> > (filter1,filter2,filter3) and return them in their original columns.
> > I've got this so far but the UNION simply lists all results under the
> > filter1 column. How can I correct this please?

>
> > SELECT DISTINCT filter1 AS filter1
> > FROM `home_index`
> > UNION
> > SELECT DISTINCT filter2 AS filter2
> > FROM `home_index`
> > UNION
> > SELECT DISTINCT filter3 AS filter3
> > FROM `home_index`

>
> > Thanks for any help!
> > Ciarán

>
> Are you looking for this?
>
> SELECT DISTINCT filter1 AS filter1, '' AS filter2, '' AS filter3
> FROM home_index
> UNION
> SELECT DISTINCT '' AS filter1, filter2 AS filter2, '' AS filter3
> FROM home_index
> UNION
> SELECT DISTINCT '' AS filter1, '' AS filter2, filter3 AS filter3
> FROM home_index
>
> --
> PleegWat
> Remove caps to reply



Yes, Thanks PleegWat - That will do nicely. It produces empty fields
but I can just filter them out with php.
Thanks for the help!
Ciarán
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-28-2008, 11:32 AM
Paul Lautman
 
Posts: n/a
Default Re: help with DISTINCT keyword

PleegWat wrote:
> On Sun, 10 Feb 2008 06:18:44 -0800, Ciaran wrote:
>
>> Hi,
>> I'm trying to select only unique values from 3 columns
>> (filter1,filter2,filter3) and return them in their original columns.
>> I've got this so far but the UNION simply lists all results under the
>> filter1 column. How can I correct this please?
>>
>> SELECT DISTINCT filter1 AS filter1
>> FROM `home_index`
>> UNION
>> SELECT DISTINCT filter2 AS filter2
>> FROM `home_index`
>> UNION
>> SELECT DISTINCT filter3 AS filter3
>> FROM `home_index`
>>
>> Thanks for any help!
>> Ciarán

>
> Are you looking for this?
>
> SELECT DISTINCT filter1 AS filter1, '' AS filter2, '' AS filter3
> FROM home_index
> UNION
> SELECT DISTINCT '' AS filter1, filter2 AS filter2, '' AS filter3
> FROM home_index
> UNION
> SELECT DISTINCT '' AS filter1, '' AS filter2, filter3 AS filter3
> FROM home_index


A "UNION" implies DISTINCT, so the DISTINCTs should not be required.


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-28-2008, 11:32 AM
PleegWat
 
Posts: n/a
Default Re: help with DISTINCT keyword

On Sun, 10 Feb 2008 07:06:40 -0800, Ciaran wrote:

> On Feb 10, 2:41 pm, PleegWat
> <pleegwat.REM...@CAPS.leegwater-68.nl.INVALID> wrote:
>> On Sun, 10 Feb 2008 06:18:44 -0800, Ciaran wrote:
>> > Hi,
>> > I'm trying to select only unique values from 3 columns
>> > (filter1,filter2,filter3) and return them in their original columns.
>> > I've got this so far but the UNION simply lists all results under the
>> > filter1 column. How can I correct this please?

>>
>> > SELECT DISTINCT filter1 AS filter1
>> > FROM `home_index`
>> > UNION
>> > SELECT DISTINCT filter2 AS filter2
>> > FROM `home_index`
>> > UNION
>> > SELECT DISTINCT filter3 AS filter3
>> > FROM `home_index`

>>
>> > Thanks for any help!
>> > Ciarán

>>
>> Are you looking for this?
>>
>> SELECT DISTINCT filter1 AS filter1, '' AS filter2, '' AS filter3 FROM
>> home_index
>> UNION
>> SELECT DISTINCT '' AS filter1, filter2 AS filter2, '' AS filter3 FROM
>> home_index
>> UNION
>> SELECT DISTINCT '' AS filter1, '' AS filter2, filter3 AS filter3 FROM
>> home_index
>>
>> --
>> PleegWat
>> Remove caps to reply

>
>
> Yes, Thanks PleegWat - That will do nicely. It produces empty fields but
> I can just filter them out with php. Thanks for the help!
> Ciarán


The field names will be the same for all rows. That's how it's designed
and there's no getting around that. If you prefer, you can use NULL
instead of '', but that won't matter much for the php side.

--
PleegWat
Remove caps to reply
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 05:27 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com