I have table questions as
id | question | question_level
______________________________________
1 | abc | 1
______________________________________
2 | prs | 3
______________________________________
3 | oesl | 2
______________________________________
4 | ocsl | 3
______________________________________
5 | qoindos | 1
______________________________________
6 | xyz | 3
______________________________________
7 | mnlop | 2
______________________________________
8 | cllse | 2
______________________________________
9 | teuosn | 4
______________________________________
10 | ulcd | 2
______________________________________
I want to select 10 records which will match with question level I have order of level in which I want all records as below
1,2,1,2,3,2,4,2,3,3
Output should be
id | question | question_level
______________________________________
1 | abc | 1
______________________________________
3 | oesl | 2
______________________________________
5 | qoindos | 1
______________________________________
7 | mnlop | 2
______________________________________
2 | prs | 3
______________________________________
8 | cllse | 2
______________________________________
9 | teuosn | 4
______________________________________
10 | ulcd | 2
______________________________________
4 | ocsl | 3
______________________________________
6 | xyz | 3
______________________________________
I tried different solutions but couldn't get correct output tried with field, find_in_set but no success. Refered [Force MySQL to return duplicates from WHERE IN clause without using JOIN/UNION? but getting only count and record in asceding order
I tried below solutions
SELECT question_level FROM `tbl_questions` WHERE `question_level` IN (1,2,1,2,3,2,4,2,3,3) ORDER BY FIELD(`question_level`, 1, 2, 1, 2, 3, 2, 4, 2, 3, 3) LIMIT 10
SELECT question_level FROM tbl_questions WHERE FIND_IN_SET(`question_level`,'1,2,1,2,3,2,4,2,3,3');
SELECT question_level,question_object_name,question_object_path,question_answer
FROM tbl_questions e JOIN (SELECT 1 AS question_level UNION ALL
SELECT 2 UNION ALL
SELECT 1 UNION ALL
SELECT 2 UNION ALL
SELECT 3 UNION ALL
SELECT 2 UNION ALL
SELECT 4 UNION ALL
SELECT 2 UNION ALL
SELECT 3 UNION ALL
SELECT 3
) matches
USING (question_level) LIMIT 10;
I tried with foreach loop also but every time getting same record when question level matches with value
$array = explode(',', '1,2,1,2,3,2,4,2,3,3')
foreach ($array as $value) {
SELECT question_level FROM tbl_questions
WHERE question_level = $value;
}
If it is not possible in mysql then can it be achieve using php.
Thanks in advance