MySQL Select Distinct

Posted on 2004-11-19
Last Modified: 2010-10-05

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";

Question by:milkmon123
    LVL 48

    Accepted Solution

    you can try using "Group By", like:

    Select field1, field2 from table1 group by field1, field2
    LVL 12

    Expert Comment


    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";


    Expert Comment


    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";


    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Join & Write a Comment

    I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
    Shadow IT is coming out of the shadows as more businesses are choosing cloud-based applications. It is now a multi-cloud world for most organizations. Simultaneously, most businesses have yet to consolidate with one cloud provider or define an offic…
    Video by: Steve
    Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now