Re: MySQL: Random select with specific count of a column
| From: | Rui Cunha | Date: | Wed, 30 Jun 2004 09:10:42 +0000 |
| Subject: | Re: MySQL: Random select with specific count of a column | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-35046@lists.php.net to get a copy of this message | ||
Hi Torsten,
until now,the best i could get was getting 6 random rows...still working on ensuring to retrive just 2 rows for category...meanwhile,you can solve the problem easily with a php loop trough the following query:
select field_id , language , category
from yourtable
where language = 'de'
group by category , field_id
order by category , rand()
limit 6;
PS1: i'm considering you're using just one table and my table structure is as follows: yourtable (field_id , language, category).
in case you're using more than 1 table, just add the required joins...
hope this helps.
Rui Cunha
Torsten Roehr writes:
"Pablo M. Rivas" <pmrivas01@yahoo.com.ar> wrote in message news:12572007801.20040629110044@yahoo.com.ar...Hello Torsten,Hi Pablo, thanks for your help. Unfortunately it's always returning the same row for each category (maybe because of the group by) and only one row for each category. I need to select 2 random rows for each category. Any more ideas? Thanks, TorstenThis is one, but I think you can find another one: Supose your table is called tablename select * from tablename as a left join tablename as b on a.category = b.category and a.name<>b.name and a.language=b.language where a.language='de' and b.name is not null group by a.category