Forum Moderators: coopster

Message Too Old, No Replies

MySQL: Limiting results to one of each type.

         

YorkshireSteve

3:56 pm on Aug 8, 2003 (gmt 0)

10+ Year Member



Hi,

I want to create a list based on each different type of data in a MySQL table. Example:


Title ¦ Surname ¦ Sex
-----------------------
Mr....¦ Smith...¦ Male
Mrs...¦ Smith...¦ Female
Mr....¦ Jones...¦ Male
Mr....¦ Robers..¦ Male
Mrs...¦ Simpson.¦ Female

In my list, I want only to display one of each type, leaving me with a list like this:


- Male
- Female

Other than looping through the results, and putting 'Male' into an array if it doesn't exist already, is there any other way to do this?

bcolflesh

4:04 pm on Aug 8, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



SELECT DISTINCT Sex
FROM yourDB
ORDER BY Sex DESC

[edited by: bcolflesh at 4:09 pm (utc) on Aug. 8, 2003]

YorkshireSteve

4:07 pm on Aug 8, 2003 (gmt 0)

10+ Year Member



That will just create me a list like this:

- Male
- Male
- Male
- Male
- Male
- Male
- Female
- Female
- Female
- Female

...which doesn't work too well in a drop-down list.

Steve.

bcolflesh

4:09 pm on Aug 8, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yeah - check my edit - is that what you want?

killroy

4:09 pm on Aug 8, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



SELECT DISTINCT Sex FROM table.

should do the trick, unless I'm misunderstanding you.

this is in MySQL, is it?

SN

YorkshireSteve

4:20 pm on Aug 8, 2003 (gmt 0)

10+ Year Member



Aha,
DISTINCT
- Perfect!

I'll have to get round to reading the MySQL manual sometime... ;o)

Thanks a lot guys,

Steve.