MySQL Select Distinct


I want to access a list of database items without duplication of one particular column.

Usually I would have "select * from products where section='$section'"

Now I have "select distinct sub_cat from products where section='$section'"...

But I want access to the other columns (e.g."select distinct sub_cat, * from products...." - but this doesn't work)

How can I do this so I can use the following..

while(list($num, $sub_cat, $image... etc)=mysql_fetch_row($res)){
   print "<img src='images/user_thumbs/".$num.".JPG' align='left'>$p_subcat";

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ryan ChongCommented:
you can try using "Group By", like:

Select field1, field2 from table1 group by field1, field2

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial

If sub_cat is not one-to-one related to $num and $image and you want to have one sub cat per img tag, then you would have to do something like this:

my $sql = "select sub_cat, max( num) as num, max(image) as image
from products
where section='$section'
group by sub_cat";


Usualy using distinct in your query should result in distinct rows that match your query, not distinct values for a specific column -- so if you have the same "sub_cat", but different products on each subcategory, distinct will only work if you ommit the other values that change.

Another workaround can be made with the scripting -- you could set all "sub_cat" that were found on a variable and check if a a value was printed before printing it -- from your code:

    $existing_sub_cat = array();
    while(list($num, $sub_cat, $image... etc)=mysql_fetch_row($res)){
        if (!in_array($sub_cat, $existing_sub_cat)) {
            $existing_sub_cat[] = $sub_cat;
            print "<img src='images/user_thumbs/".$num.".JPG' align='left'>$p_subcat";

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.