hello everybody! I need a little suggestion or help on how could I extract 3 rows from mysql at the same time, 2 being randomly selected and one not. the problem I'm struggling: I intend to make a quizz, I have a database (rows: id, question, correct_answer) I want to generate 3 answers automatically, from them 1 will be true and 2 fake -- randomly selected from other questions' correct_answers. hope made myself understandable, maybe someone's facing this problem too. thanks for your time
(SELECT correct_answer FROM mytable WHERE question=X) UNION (SELECT correct_answer FROM mytable WHERE question IS NOT X ORDER BY RAND() LIMIT 2) ORDER BY BY RAND(); Should by roughly what you are looking for. First part of the union gets the correct answer, the second part gets two other answers and then the three are ordered random.
thanks chisara, I eddited and modified it a bit, because my database is a little bit more than I showed here, but it works! great tip!
sorry to dig this up but i want to use the same $sql= " (SELECT * FROM table WHERE id = 131) UNION ( SELECT * FROM table ORDER BY RAND())"; $query_limit_rsImages = sprintf("%s LIMIT %d, %d", $sql, $startRow_rsImages, $maxRows_rsImages); PHP: Would the LIMIT be the cause of strange behavior?
Since my php/sprintf knowledge is non existent. Could you post the SQL statements that are send into the database server ? P.S. What is your strange behaviour?