Tuesday, 15 May 2012

MYSQL SELECT DISTINCT RECORDS GROUPED -


I need to select "four different random records" from the specified four categories and then according to ascending order.

I tried to choose sixteen random records and then I grouped them by category_ id

query:

  

Results in a few moments:

  id category_ id description rnd -------------------------- ----- ------------------------------- 224 1 Question 7 0.004305024635330797 293 2 Questions 10 0.006966075866451558 601 3 Question 2 0. 001877430828174046 958 4 Questions 54 0.0065207639769844375   

Results in other moments:

  id category_id Description rnd ------- ----- --------------------------------------------- ----- 230 1 Question 2 0.01622675640157122 310 2 Questions 21 0.005430353810480194 15 9 4 Question 17 0.021778853630441106   

The problem is that I have not always shown four categories to fix this query So far, I can not find the real solution.
I need your help!
Thanks in advance!

Select the key categories first and then go back to the original data:

  Select q From category_id, substring_index (group_concat (id) order by rand (id)), ',', 1) query by category_id Question range by group 4) Enter qq at q4 q.id = c4.id Category_ id There are other ways to do this, such as using a  union all  statement but it is normal and makes changing the number of categories easier.   

No comments:

Post a Comment