Ambil saja 2 per kategori seperti yang Anda jelaskan, dan satu acak di akhir. Ini bukan satu kueri, tetapi satu kumpulan hasil, yang mungkin Anda butuhkan:
SELECT * FROM (SELECT * FROM questions WHERE category= 1 ORDER BY rand() limit 0,2) as t1
UNION
SELECT * FROM (SELECT * FROM questions WHERE category= 2 ORDER BY rand() limit 0,2) as t2
UNION
SELECT * FROM (SELECT * FROM questions WHERE category= 3 ORDER BY rand() limit 0,2) as t3
UNION
...
(Pilihan bersarang memungkinkan Anda mengurutkan berdasarkan Rand() per kategori) Sejauh ini tidak ada yang istimewa - 2 pertanyaan acak per kategori.
Bagian yang sulit sekarang adalah menambahkan elemen ke-15 TANPA memilih salah satu yang sudah Anda miliki.
Untuk mencapainya dengan panggilan "satu", Anda dapat melakukan hal berikut:
- Ambil bagian dari 14 pertanyaan yang telah Anda pilih seperti di atas.
- Satukan ini dengan sekumpulan hal yang diurutkan secara acak yang tidak dikategorikan dari database. (batas 0,15)
-
Pilih semua dari hasil ini, batasi 0,15.
-
JIKA 14 elemen pertama dari subkueri TERAKHIR sudah dipilih - elemen tersebut akan dihapus karena
UNION
, dan elemen ke-15 yang independen dijamin. - Jika kueri dalam terakhir juga memilih 15 pertanyaan berbeda, batas luar 0,15 hanya akan memasukkan pertanyaan pertama ke dalam hasil.
Sesuatu seperti:
SELECT * FROM (
SELECT * FROM (SELECT * FROM questions WHERE category= 1 ORDER BY rand() limit 0,2) as t1
UNION
SELECT * FROM (SELECT * FROM questions WHERE category= 2 ORDER BY rand() limit 0,2) as t2
UNION
SELECT * FROM (SELECT * FROM questions WHERE category= 3 ORDER BY rand() limit 0,2) as t3
UNION
...
UNION
SELECT * FROM (SELECT * FROM questions ORDER BY rand() LIMIT 0,15) as t8
) AS tx LIMIT 0,15
Ini agak jelek, tetapi harus melakukan apa yang Anda butuhkan:2 pertanyaan acak dari SETIAP kategori, dan akhirnya pertanyaan acak yang belum dipilih dari kategori APAPUN. Total 15 pertanyaan setiap saat.
(Sidenode:Anda juga dapat menjalankan kueri kedua, menggunakan NOT IN ()
untuk melarang pertanyaan yang sudah dipilih setelah menentukan 14 pertanyaan untuk 7 kategori.)
Sunting:Sayangnya SQL Fiddle tidak berfungsi saat ini. Berikut beberapa kode biola:
CREATE TABLE questions (id int(10), category int(10), question varchar(20));
INSERT INTO questions (id, category, question)VALUES(1,1,"Q1");
INSERT INTO questions (id, category, question)VALUES(2,1,"Q2");
INSERT INTO questions (id, category, question)VALUES(3,1,"Q3");
INSERT INTO questions (id, category, question)VALUES(4,2,"Q4");
INSERT INTO questions (id, category, question)VALUES(5,2,"Q5");
INSERT INTO questions (id, category, question)VALUES(6,2,"Q6");
INSERT INTO questions (id, category, question)VALUES(7,3,"Q7");
INSERT INTO questions (id, category, question)VALUES(8,3,"Q8");
INSERT INTO questions (id, category, question)VALUES(9,3,"Q9");
INSERT INTO questions (id, category, question)VALUES(10,4,"Q10");
INSERT INTO questions (id, category, question)VALUES(11,4,"Q11");
INSERT INTO questions (id, category, question)VALUES(12,4,"Q12");
INSERT INTO questions (id, category, question)VALUES(13,5,"Q13");
INSERT INTO questions (id, category, question)VALUES(14,5,"Q14");
INSERT INTO questions (id, category, question)VALUES(15,5,"Q15");
INSERT INTO questions (id, category, question)VALUES(16,6,"Q16");
INSERT INTO questions (id, category, question)VALUES(17,6,"Q17");
INSERT INTO questions (id, category, question)VALUES(18,6,"Q18");
INSERT INTO questions (id, category, question)VALUES(19,7,"Q19");
INSERT INTO questions (id, category, question)VALUES(20,7,"Q20");
INSERT INTO questions (id, category, question)VALUES(21,7,"Q21");
Kueri
SELECT * FROM (
SELECT * FROM (SELECT * FROM questions WHERE category= 1 ORDER BY rand() limit 0,2) as t1
UNION
SELECT * FROM (SELECT * FROM questions WHERE category= 2 ORDER BY rand() limit 0,2) as t2
UNION
SELECT * FROM (SELECT * FROM questions WHERE category= 3 ORDER BY rand() limit 0,2) as t3
UNION
SELECT * FROM (SELECT * FROM questions WHERE category= 4 ORDER BY rand() limit 0,2) as t4
UNION
SELECT * FROM (SELECT * FROM questions WHERE category= 5 ORDER BY rand() limit 0,2) as t5
UNION
SELECT * FROM (SELECT * FROM questions WHERE category= 6 ORDER BY rand() limit 0,2) as t6
UNION
SELECT * FROM (SELECT * FROM questions WHERE category= 7 ORDER BY rand() limit 0,2) as t7
UNION
SELECT * FROM (SELECT * FROM questions ORDER BY rand() LIMIT 0,15) as t8
) AS tx LIMIT 0,15
contoh data berisi 3 pertanyaan per jenis, yang mengarah ke hasil bahwa pertanyaan ke-15 (baris terakhir) SELALU pertanyaan yang tersisa dari suatu kategori.