Mysql
 sql >> Database >  >> RDS >> Mysql

Come utilizzare la funzione Rand combinata con Union in MySQL

Potresti creare un SQL esterno in cui la clausola from è una sorta di tabella temporanea come questa:

 SELECT food_name from (
  SELECT foodName
  FROM food
  WHERE foodCategory_id = 1 OR 2
  ORDER BY RAND() limit 1) a
 UNION
 SELECT food_name from (
  SELECT foodName
  FROM food
  WHERE foodCategory_id = 7 OR 8 OR 9
  ORDER BY RAND() limit 1) b
 UNION
 SELECT food_name from (
  SELECT foodName
  FROM food
  WHERE foodCategory_id = 10 OR 11
  ORDER BY RAND() limit 1) c