dbTalk Databases Forums  

A Question about DISTINCT.

comp.databases.mysql comp.databases.mysql


Discuss A Question about DISTINCT. in the comp.databases.mysql forum.



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

Default A Question about DISTINCT. - 01-30-2012 , 03:51 PM






Assuming you have a table like:

X Y Z
-------
A, B, C
A, B, C
A, A, C
A, A, A
A, A, A

select DISTINCT X, Y, Z FROM table;
A, B, C
A, A, C
A, A, A

is this different than
select DISTINCT(X), Y, Z from table;

and why doesn't this work?
select X, DISTINCT(Y), Z from table;

Reply With Quote
  #2  
Old   
Thomas 'PointedEars' Lahn
 
Posts: n/a

Default Re: A Question about DISTINCT. - 01-30-2012 , 04:36 PM






SpreadTooThin wrote:

Quote:
Assuming you have a table like:

X Y Z
-------
A, B, C
A, B, C
A, A, C
A, A, A
A, A, A

select DISTINCT X, Y, Z FROM table;
A, B, C
A, A, C
A, A, A

is this different than
select DISTINCT(X), Y, Z from table;
No. As a peculiar result of tokenizing, this is parsed as if it was

select DISTINCT (X), Y, Z from table;

which is equivalent to the first statement.

Quote:
and why doesn't this work?
select X, DISTINCT(Y), Z from table;
Because it is not proper (My)SQL. DISTINCT is a *keyword*, not a function;
if present, it MUST immediately follow the SELECT keyword of the SELECT
statement.

For example,

SELECT DISTINCT(*) FROM table;

is a syntax error as well, while

SELECT DISTINCT * FROM table;

is not.

RTFM: <http://dev.mysql.com/doc/refman/5.5/en/select.html>

--
PointedEars

Please do not Cc: me. / Bitte keine Kopien per E-Mail.

Reply With Quote
  #3  
Old   
Gordon Burditt
 
Posts: n/a

Default Re: A Question about DISTINCT. - 01-31-2012 , 09:05 PM



Quote:
select DISTINCT X, Y, Z FROM table;
A, B, C
A, A, C
A, A, A

is this different than
select DISTINCT(X), Y, Z from table;

and why doesn't this work?
select X, DISTINCT(Y), Z from table;
You get to DISTINCT or not DISTINCT. You don't get to DISTINCT some
of the fields and not others. All those parentheses are meaningless.

Look at GROUP BY. DISTINCT is (usually) equivalent to GROUP BY all
of the selected fields, the exceptions involving clauses not shown here.

SELECT DISTINCT (((((((((x))))))))), (((y))), (z) FROM table
is equivalent to:
SELECT x, y, z FROM table GROUP BY x, y, z

What you wish:
SELECT x, DISTINCT(y), z from table;
did (it is a syntax error) is equivalent to:
SELECT x, y, z FROM table GROUP BY y;
In standard SQL selecting x and z without making them part of the
GROUP BY (or used in aggregate functions) as in the statement above
is an error.

Beware that in this statement, x and z are taken from one of the
records in the group (not necessarily the *same* record for x and
z) and you don't get to chose which one (except by using aggregate
functions like max(x)). This can be confusing.

Reply With Quote
  #4  
Old   
Denis McMahon
 
Posts: n/a

Default Re: A Question about DISTINCT. - 02-01-2012 , 10:04 AM



On Mon, 30 Jan 2012 13:51:33 -0800, SpreadTooThin wrote:

Quote:
select DISTINCT X, Y, Z FROM table;

is this different than
select DISTINCT(X), Y, Z from table;
No

Quote:
and why doesn't this work?
select X, DISTINCT(Y), Z from table;
Because it's not syntactically correct, see eg:

http://dev.mysql.com/doc/refman/5.1/en/select.html

Note that DISTINCT comes *before* any list of columns.

Also:

"The ALL and DISTINCT options specify whether duplicate rows should be
returned. ALL (the default) specifies that all matching rows should be
returned, including duplicates. DISTINCT specifies removal of duplicate
rows from the result set. It is an error to specify both options.
DISTINCTROW is a synonym for DISTINCT."

This also implies that DISTINCT applies at the row level, not at the
column level. Specifically "DISTINCT specifies removal of duplicate rows
from the result set."

The answers that you seek are in the documentation that is provided.
Perhaps you could read it.

Rgds

Denis McMahon

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.