Search This Blog

Wednesday, September 7, 2011

HowTo: MySQL SELECT * FROM TABLE WHERE DISTINCT `field`

MySQL :: MySQL 5.0 Reference Manual :: 12.2.8 SELECT Syntax

Web applications need to allow end users to select groups of rows by project, product or something interesting. So the best way to do that is to ask the database for a list of unique groups. Unfortunately, MySQL syntax doesn't have one, and the journey can lead to some commands that are downright scary for a simple database kind of thing to do.

SELECT * , count( `product` )
FROM `results`
GROUP BY `product`
HAVING count( `product` ) >=1
LIMIT 0 , 30


and bug the mysql people to add:
SELECT * FROM `results` WHERE DISTINCT `product` ;)

3 comments:

  1. It works very good. Good ideea this post. Thanks, I apreciated it a lot

    ReplyDelete
  2. Thx for this script. I am looking for this one. BEST

    ReplyDelete
  3. Great man, it helped me a lot !! amd you're right!!: mysql people have to add:
    SELECT * FROM `results` WHERE DISTINCT `product`

    ReplyDelete