$db=getDB();
$result=mysql_query('SELECT question,answer,cid FROM questions JOIN (SELECT CEIL(RAND() * (SELECT MAX(uid) FROM questions)) AS uid ) AS r2 USING (uid);',$db);
$row=mysql_fetch_array($result);
mysql_free_result($result);
$sql='SELECT name FROM categories where uid='.$row{"cid"}.' ORDER BY name LIMIT 1;';
$result=mysql_query($sql,$db);
$row2=mysql_fetch_array($result);
mysql_free_result($result);
$row{"name"}=$row2{"name"};
The first join does a super efficient job of selecting a random record.
But how to combine the second so that I only have to do one single query?