mercredi 8 mars 2017

Random function in WITH clause doesn't work properly

I'm using the random function to get a random number between 1 and 5. To achieve this I'm using the WITH clause to make sure that the random function is called only once.

WITH search_exam(exam) AS (
SELECT ABS(RANDOM()) % (6-1) + 1
),
this_exam AS (Select Question._id, question_text, question_type, topic, favorite, picture_text, picture_src, video_text, video_src, topic_text, exam
FROM Question 
LEFT JOIN Question_Lv ON Question._id = Question_Lv.question_id 
LEFT JOIN Picture ON Question._id = Picture.question_id 
LEFT JOIN Picture_Lv ON Question._id = Picture_Lv.question_id 
LEFT JOIN Video ON Question._id = Video.question_id
LEFT JOIN Video_Lv ON Question._id = Video_Lv.question_id 
LEFT JOIN Topic ON Question.topic = Topic._id 
LEFT JOIN Topic_Lv ON Topic._id =Topic_Lv.topic_id 
LEFT JOIN Exam ON Question._id = Exam.question_id)

SELECT * FROM this_exam WHERE exam = (SELECT exam FROM search_exam);

So the problem is that the result is often empty (without any error). And then if I execute the query few times again it gives me the correct result.

But the random function works perfect. If I execute

SELECT ABS(RANDOM()) % (6-1) + 1

I always get a number between 1 and 5. So I don't understand this strange behaviour of this query.

Where is the problem?




Aucun commentaire:

Enregistrer un commentaire