Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to query 10 random unique records in mysql database? [duplicate]

Tags:

mysql

Possible Duplicate:
how to select random unique records on each execution of the SQL Query

I have database of that structure:

id int
image_name varchar(200)
category_id int

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

Could you help me to get a query, which will give me 10 records with UNIQUE category_ids?

like image 315
pawel Avatar asked Aug 23 '12 08:08

pawel


1 Answers

select DISTINCT(category),id,image_name FROM images 
  WHERE id=
    (FLOOR(RAND() * 
           (SELECT COUNT(*) FROM images )
          )
    );
like image 174
Mihai Labo Avatar answered Sep 29 '22 00:09

Mihai Labo