[SOLVED] Using DISTINCT function..

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
raymedia
Forum Commoner
Posts: 27
Joined: Thu Oct 09, 2003 6:36 am
Location: Melbourne, Australia

[SOLVED] Using DISTINCT function..

Post by raymedia »

Hi guys..hows going. Just need some help in mysql syntax.

I need to select two fields (field1 and field2) in table "mytable" and A have to be distinct.

So i wrote,

SELECT DISTINCT field1, field2 FROM mytable ORDER BY field1

and all it does is selecting all field1, field2 including duplicates. I did some reseach online but no luck. Can any SQL gurus help me..Please...Thank you
User avatar
Wayne
Forum Contributor
Posts: 339
Joined: Wed Jun 05, 2002 10:59 am

Post by Wayne »

Dont quite understand what you are trying to do, but I am guessing something like

Code: Select all

SELECT DISTINCT field1, field2 FROM mytable GROUP BY field1 ORDER BY field1
you might want to explain it a little better, if thats not what you are trying to do.
fractalvibes
Forum Contributor
Posts: 335
Joined: Thu Sep 26, 2002 6:14 pm
Location: Waco, Texas

Post by fractalvibes »

The sql you have will return every distinct combnation of field1 and field2.
so if your data looks like:

f1 f2
a b
a b
c r
e f
a c
e f

you will get a b, c r, e f, and a c returned.

fv
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

Post by andre_c »

Maybe this is what you want?

Code: Select all

SELECT field1, field2 FROM mytable GROUP BY field1
User avatar
raymedia
Forum Commoner
Posts: 27
Joined: Thu Oct 09, 2003 6:36 am
Location: Melbourne, Australia

Post by raymedia »

Code: Select all

SELECT DISTINCT field1, field2 FROM mytable GROUP BY field1 ORDER BY field1
solved my problem..thank you all for your help
Post Reply