How to query 10 random unique records in mysql database?

Possible duplicate:
how to select random unique records each time an SQL query is executed

I have a database of this structure:

id int
image_name varchar(200)
category_id int

There are about 200 entries, id is unique, and there are about 20 categories, and my iamges are divided between them.

Could you help me get a query that will give me 10 entries using UNIQUE category_ids?

+5
source share
3 answers
select DISTINCT(category),id,image_name FROM images 
  WHERE id=
    (FLOOR(RAND() * 
           (SELECT COUNT(*) FROM images )
          )
    );
+7
source
SELECT DISTINCT(category),id,image_name FROM images ORDER BY RAND() LIMIT 10
+2
source
SELECT DISTINCT category_id,id,image_name FROM images ORDER BY RAND() LIMIT 10
+1
source

All Articles