SQL 查询注意事项

1,多条件排序用“,”来执行,

例子:

以Cno升序、Degree降序查询Score表的所有记录。
select * from Score order by Cno,Degree desc

2,SQL server 没有limit语句但有类似的top语句例子

1 select top 1 Sno,Cno from Score
2 order by Degree desc

3 表格之间的联结不一定需要有相同的列只要有对应关系就可以,例如一个表格有成绩列,另一个表格是等级列也可以联结例如

--现查询所有同学的Sno、Cno和rank列。
--方法一
select * , (select ranks from grade where low < Degree and Upp >= Degree) as 评级
from Score
方法二
select A.* , G.ranks from Score as A 
inner join grade as G
on A.Degree between G.low and G.upp

4,子查询的注意事项,关联子查询可以在细分的组内进行比较时使用

--20、查询score中选学多门课程的同学中分数为非最高分成绩的记录。
Select * from score a 
where degree <(select max(degree) from Score b where b.Cno=a.Cno) 
and Sno in (select Sno from Score group by Sno having count(*)>1)

--结合条件一定要写在子查询中,子查询内部设定的关联名称,只能在该子查询内部使用,也就是说内部可以看到外部,而
--外部看不到内部
--SQL是按照先内层子查询后补外层查询的顺序来执行的,这样,子查询执行结束后只会留下执行结果.

  

猜你喜欢

转载自www.cnblogs.com/zhangxudong-cnblogs/p/10964412.html
今日推荐