Thursday, 5 September 2013

Random Insert into a second table

Random Insert into a second table

I would like to pull 10 random records from a MySql table (questions) and
insert them into another table (active), but only records which are not
already in the second table (active). The code I have works if the second
table is not empty, but does not give any results at all if the table IS
empty. Can anybody see why and what I can do please?
INSERT INTO active (quesindex)
(
SELECT DISTINCT(questions.quesindex)
FROM questions, (
SELECT questions.quesindex AS sid
FROM questions, active
where questions.quesindex NOT IN (SELECT active.quesindex FROM active )
ORDER BY RAND( )
LIMIT 10
) tmp
WHERE questions.quesindex = tmp.sid
)

No comments:

Post a Comment