且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何按照唯一且预定的顺序(而不是asc或desc)对来自mysql数据库的数据进行排序

更新时间:2023-10-08 09:47:52

此外,如果可以帮助找到一个 解决方案.

Also, I can change the db structure if that would help find a solution.

如果可以更改db结构,则不必将排序顺序存储为用管道分隔的字符串,而应将其存储在一个单独的表中,该表将每个问题映射到给定学生应出现的顺序.即

If changing the db structure is possible, then instead of storing the sorting order as a pipe separated string, store it in a separate table that maps each question to the order it should appear in for a given student. i.e.

student_id, sort_order, question_id
1               1               8
1               2               2
1               3               97

然后在为特定学生选择问题时加入排序表.

Then join on your sorting table when selecting your questions for a particular student.

SELECT q.* FROM 
questions q
JOIN questions_sorting_order qso
ON q.id = qso.question_id
ORDER BY qso.sort_order
WHERE qso.student_id = :student_id