Anda bisa mendapatkan 10 elemen acak per jenis dengan kueri seperti ini:
select word from YOUR_TABLE where type = 'noun' order by rand() limit 10;
select word from YOUR_TABLE where type = 'adj' order by rand() limit 10;
dan kemudian menempatkan mereka bersama-sama dalam kode PHP Anda, seperti:
$phrases = array();
$adj_result = mysql_query("SELECT word FROM words WHERE type = 'adj' ORDER BY RAND() LIMIT 10");
$noun_result = mysql_query("SELECT word FROM words WHERE type = 'noun' ORDER BY RAND() LIMIT 10");
while($adj_row = mysql_fetch_assoc($adj_result)) {
$noun_row = mysql_fetch_assoc($noun_result);
$phrases[$adj_row['word']] = $noun_row['word'];
}
print_r($phrases);
Harap perhatikan bahwa kode ini tidak terlalu aman (itu membuat asumsi bahwa kueri kedua selalu menghasilkan hasil paling sedikit sebanyak yang pertama), tetapi Anda mendapatkan idenya.
Sunting:Inilah satu kueri SQL yang harus melakukannya:
select t1.word, t2.word
from
((select word from YOURTABLE where type = 'adj' order by rand()) as t1),
((select word from YOURTABLE where type = 'noun' order by rand()) as t2)
order by rand()
limit 10;
EDIT:contoh yang dihapus