ok I bugged my head on this the whole day today and can't get this to work, basically I'm trying to query randomly 12 photos from a temp table which contains all photos the active logged-in user not yet voted on.
The error is obviously #1137 - Can't reopen table: 'r1' which temp table can only be reference once.
CREATE TEMPORARY TABLE unvoted_photos
SELECT *
FROM photo
WHERE photo.photo_uid
NOT IN
( SELECT photo_uid
FROM vote
WHERE vote.user_uid = '12345' ) ;
SELECT photo_uid, caption, storage_path
FROM unvoted_photos AS r1
JOIN (SELECT (RAND() *
(SELECT MAX(id)
FROM unvoted_photos)) AS sid)
AS r2
WHERE r1.id >= r2.id
ORDER BY r1.id ASC
LIMIT 12;
The random select query followed this example due to performance issue. Otherwise this certainly would work.
SELECT ... FROM photo WHERE photo_uid NOT IN ( voted images subquery ) ORDER BY RAND() LIMIT 12