Forum Moderators: mack

Message Too Old, No Replies

Help: Using SQL Query to show duplicates

Help: Using SQL Query to show duplicates

         

thekid

2:34 am on Oct 31, 2006 (gmt 0)

10+ Year Member



Hello!

Need a bit of help figuring this one out.

I have a table in a database and one column is named "Type"

I want to look at all the "Types" and print out only ONE of each type in a dyanmic list.

ie:
-------
¦ Type ¦
-------
¦ Car ¦
-------
¦ Car ¦
-------
¦ Truck ¦
-------
¦ Car ¦
-------

Then the list would look like:

Car
Truck

I can figure out the PHP to make the list, just not sure what the query and subsquent code to get the list values out.

Does that make sense? Can anyone help me out with this one?

Thanks!

Mike

jtara

5:11 am on Oct 31, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here's the SQL:

select distinct type from table order by type;

You should have an index on "type", or else this could be very slow. (It would have to read every row of the database.) On the other hand, if you have an index on "type" it will be VERY fast.

thekid

12:24 am on Nov 1, 2006 (gmt 0)

10+ Year Member



Okay,

Thanks for the response.

I tried that, but I don't think its working...heres a more specific example.

My Table is a list of Vendors with the usual information (Name Address Etc.)

The VENDORTYPE Column is currently populated with the types AUDIO and and VIDEO but as the database grows there may be more entries.

When I used your script:

$result = mysql_query("SELECT DISTINCT VENDORTYPE FROM VENDOR ORDER BY VENDORTYPE");

$types = mysql_fetch_array($result);

print_r ($types);

I expected Array ([0] = > Audio [1] = > Video)

what I got was:

Array ([0] => Audio [VENDORTYPE] = > Audio)

The video portion never showed up at all....what am I missing?

Appreciate the help very much!

Mike

jtara

1:01 am on Nov 1, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



mysql_fetch_array only fetches one row of the result.

You will need to loop over the rows until you do not get any more results.

thekid

2:34 am on Nov 1, 2006 (gmt 0)

10+ Year Member



Heya!

Thanks muchly...that was simple but it worked.

Mike