dbTalk Databases Forums  

SELECT DISTINCT

comp.databases.mysql comp.databases.mysql


Discuss SELECT DISTINCT in the comp.databases.mysql forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
bruce
 
Posts: n/a

Default SELECT DISTINCT - 01-14-2011 , 03:09 PM






I have 3 tables all with the data field (field1). I want to extract a
recordset with distinct values from these tables BUT I do not want
null or blanks to appear in the recordset. The following is what I am
using (failing with).

SELECT DISTINCT field1 FROM DB1
UNION
SELECT DISTINCT field1 FROM DB2
UNION
SELECT DISTINCT field1 FROM DB3 WHERE field1 != "(null)" AND field1 !
=""

If I drop the WHERE clause, the select works but, of course, I get
nulls.l

Appreciate suggestions on this one.

Thank you..

Bruce

Reply With Quote
  #2  
Old   
Lennart Jonsson
 
Posts: n/a

Default Re: SELECT DISTINCT - 01-14-2011 , 04:13 PM






On 2011-01-14 22:09, bruce wrote:
Quote:
I have 3 tables all with the data field (field1). I want to extract a
recordset with distinct values from these tables BUT I do not want
null or blanks to appear in the recordset. The following is what I am
using (failing with).

SELECT DISTINCT field1 FROM DB1
UNION
SELECT DISTINCT field1 FROM DB2
UNION
SELECT DISTINCT field1 FROM DB3 WHERE field1 != "(null)" AND field1 !
=""

If I drop the WHERE clause, the select works but, of course, I get
nulls.l

I'm not clear on whether you want to avoid null or '(null)' but I'll
assume null. Null equals nothing, not even null so you cannot use for
example = to test for null. There is a special predicate IS [NOT] NULL
that can be used to compare against null:

SELECT DISTINCT field1 FROM DB3
WHERE field1 IS NOT NULL
AND field1 !=""

FYI, != is not standard even though many dbms accepts it, you can use <>
instead. Also ' is the standard string delimiter, and finally union will
itself remove duplicates, so there is no point in using distinct:

SELECT field1 FROM DB1
UNION
SELECT field1 FROM DB2
UNION
SELECT field1 FROM DB3
WHERE field1 IS NOT NULL
AND field1 <> ''

or

SELECT field1 FROM DB1
UNION
SELECT field1 FROM DB2
UNION
SELECT field1 FROM DB3
WHERE coalesce(field1, '') <> ''

/Lennart

Reply With Quote
  #3  
Old   
bruce
 
Posts: n/a

Default Re: SELECT DISTINCT - 01-15-2011 , 08:35 PM



On Jan 14, 5:13*pm, Lennart Jonsson <erik.lennart.jons... (AT) gmail (DOT) com>
wrote:
Quote:
On 2011-01-14 22:09, bruce wrote:

I have 3 tables all with the data field (field1). I want to extract a
recordset with distinct values from these tables BUT I do not want
null or blanks to appear in the recordset. The following is what I am
using (failing with).

SELECT DISTINCT field1 FROM DB1
UNION
SELECT DISTINCT field1 FROM DB2
UNION
SELECT DISTINCT field1 FROM DB3 WHERE field1 != "(null)" AND field1 !
=""

If I drop the WHERE clause, the select works but, of course, I get
nulls.l

I'm not clear on whether you want to avoid null or '(null)' but I'll
assume null. Null equals nothing, not even null so you cannot use for
example = to test for null. There is a special predicate IS [NOT] NULL
that can be used to compare against null:

SELECT DISTINCT field1 FROM DB3
WHERE field1 IS NOT NULL
* AND field1 !=""

FYI, != is not standard even though many dbms accepts it, you can use
instead. Also ' is the standard string delimiter, and finally union will
itself remove duplicates, so there is no point in using distinct:

SELECT field1 FROM DB1
UNION
SELECT field1 FROM DB2
UNION
SELECT field1 FROM DB3
WHERE field1 IS NOT NULL
* AND field1 <> ''

or

SELECT field1 FROM DB1
UNION
SELECT field1 FROM DB2
UNION
SELECT field1 FROM DB3
WHERE coalesce(field1, '') <> ''

/Lennart
Thank you very much. Your changes work like a charm. I've changed !=
to <>, I've dropped DISTINCT, and changed the test for null to "IS NOT
NULL". Also, changed my quote to a single quote mark.

Thanks again...

Bruce

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



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.