How to limit multiple numbers by filtering multiple columns on MySql?

前端 未结 1 1685
感动是毒
感动是毒 2021-01-24 10:55

I would like to get 50 questions from 4 different categories from the database. I want a different number of questions from each of the 4 different categories; my result set mus

相关标签:
1条回答
  • 2021-01-24 11:58

    You can use a UNION query to get all your questions at once:

    (SELECT * FROM question WHERE categori_id=1 ORDER BY RAND() LIMIT 12)
    UNION
    (SELECT * FROM question WHERE categori_id=2 ORDER BY RAND() LIMIT 20)
    UNION
    (SELECT * FROM question WHERE categori_id=3 ORDER BY RAND() LIMIT 10)
    UNION
    (SELECT * FROM question WHERE categori_id=4 ORDER BY RAND() LIMIT 8)
    
    0 讨论(0)
提交回复
热议问题