Select at least one from each category?

后端 未结 3 1129
闹比i
闹比i 2021-02-09 07:29

SQLFiddle Link

I\'ve got an SQLite database with a bunch of test/exam questions. Each question belongs to one question category.

3条回答
  •  青春惊慌失措
    2021-02-09 07:45

    Since it's sqlite (thus local): How slow would it be to just query until you have 5 answers and four different categories, dropping the duplicate category rows each iteration.

    I think, if each category is equally represented, that it would be highly unlikely that you need more than 3 iterations which should still be below a second.

    It's not algorithmically nice, but to me using random() in a SQL statement isn't algorithmically nice anyway.

提交回复
热议问题