在sql语句中的查询语句,不同的写入顺序会导致查询的结果不一样
比如我要查询一个联合表中的同学,如果查询的顺序不一样的话,那么结果就会不同
上图为数据的关系图
下面我们要查询张三老师所教的学生
select ROW_NUMBER() over(order by s.stu_id) as 序号,s.stu_id,c.class_name,s.student_name,l.lession_name
from students s,lession l,start_class sc,xuanke x,class c,teacher t
where s.class_id=c.class_id and sc.teacher_id=t.teacher_id and x.stu_id=s.stu_id
and sc.lession_id=l.lession_id and x.start_class_id=sc.start_class_id
and t.tea_id_number= 123457
group by c.class_name,l.lession_name,s.student_name,s.stu_id
结果为
我们可以看到,这次的结果是根据stu_id的结果来排序的,让我们看另一个代码
select ROW_NUMBER() over(order by x.xuanke_id) as 序号,x.xuanke_id,c.class_name,s.student_name,l.lession_name from students s,lession l,start_class sc,xuanke x,class c,teacher t where s.class_id=c.class_id and sc.teacher_id=t.teacher_id and x.stu_id=s.stu_id and sc.lession_id=l.lession_id and x.start_class_id=sc.start_class_id and t.tea_id_number= 123457 group by c.class_name,l.lession_name,s.student_name,l.lession_id,x.xuanke_id
结果为
可以看到,这次的排序结果是根据xuanke_id 来决定的
总结:
你查询的最开始的条件是什么,那么查询的结果将是按照这个排序,如果不一样的排序道德话,课用order by