以下是2个表格:
类
academic_year_id student_id standard
2 1 10
2 2 10
2 3 10
2 4 10
2 5 10
2 6 10
2 7 11 Science
2 8 11 Science
此处标准不是INT
学生
student_id roll_no name teacher_approval
1 0 S Sumeet G 1
2 0 Nair Nikhil R 1
3 0 Nayak Ankita R 0
4 0 Rathod Ketan P 0
5 0 Patel Vishal D 1
6 0 Patel Jignesh R 0
7 0 Prajapati Bhavesh A 1
8 0 Shah Harsh N 1
我想做什么:
当教师选择标准时 - > 10并按下“分配卷号”按钮
我想要所有第10个标准的学生按名称顺序给予滚动,并且由老师批准,即teacher_approval ='1'
所以我的学生表如下:
学生
student_id roll_no name teacher_approval
1 3 S Sumeet G 1
2 1 Nair Nikhil R 1
3 0 Nayak Ankita R 0
4 0 Rathod Ketan P 0
5 2 Patel Vishal D 1
6 0 Patel Jignesh R 0
7 0 Prajapati Bhavesh A 1
8 0 Shah Harsh N 1
我到目前为止尝试了以下代码:
$standard = $_POST['standard']
SET @incr = 0
UPDATE
student AS st
JOIN
( SELECT * FROM
classes AS classes
WHERE standard = '".$standard."'
) AS tmp
ON tmp.student_id = st.id
SET
st.roll_no = @incr:=@incr+1
WHERE st.teacher_approval = '1'
ORDER BY st.name ASC
但是它给了我一个错误:错误地使用了更新和订单
有类似经历的人吗?任何帮助,将不胜感激。感谢。
答案 0 :(得分:1)
在MySql中,当使用多个表时,您不能直接将ORDER BY
作为UPDATE
的一部分(请参阅此link)。试试这个:
UPDATE student AS table1
INNER JOIN (
SELECT st.student_id, st.roll_no, st.name, st.teacher_approval
FROM student AS st
JOIN (
SELECT * FROM
classes AS cl
WHERE cl.standard = '".$standard."'
) AS tmp
ON tmp.student_id = st.student_id
WHERE st.teacher_approval = '1'
ORDER BY st.name ASC
) AS table2
ON table2.student_id = table1.student_id
SET table1.roll_no = @incr:=@incr+1
希望这有帮助。