sql语句(Oracle和mysql)的注意事项

我们在平时写sql语句的时候很容易忽视一些细节问题,而且在Oracle和mysql中sql语句也是有很多的不同之处,这些我们都需要注意,下面我就把平时比较容易忽视的几个点列出来。
一:分页
mysql 分页:select * form 表名 limit m,n;
Oracel中分页:
(1)SELECT * FROM( SELECT A., ROWNUM RN FROM (SELECT FROM TABLE_NAME) A WHERE ROWNUM <= 40) WHERE RN >= 21
(2)SELECT * FROM( SELECT A., ROWNUM RN FROM (SELECT FROM TABLE_NAME) A ) WHERE RN BETWEEN 21 AND 40
(3)select * from(select a.,rownum rn from (select from t_articles) a where rownum < 11) where rn>5
或: select * from ( select t.*,rownum rn from tb_user t where rownum < 4 ) where rn > 1
二:分组
select service_name,url, count() from tb_interface_config group by service_name,url having count()>1;
select count(distinct deptno) from user;
注意1:分组时前面有几个字段group by后也要有几个字段。
注意2:使用count统计行数的时候,count()括号里面的可以写*或者任意的数字,结果都是一样的。
注意3:所有聚集函数都可以使用distinct
注意4: 一张表,一旦分组 完成后,查询后只能得到组相关的信息。
注意5:order by 和group by 前面where可有可无,而且前面不能加and。

三:where和having
select Student.S#,Student.Sname from Student,SC where Student.S#=SC.S# group by Student.S#,Student.Sname having count(C#) <(select count(C#) from Course);
四:外连接
select * from a left inner join b on a.a=b.b right inner join c on a.a=c.c inner join d on a.a=d.d where …..
五: 当字段值为null的时候把值赋为0;
select S#,avg(isnull(score,0)) from SC where S# in (select S# from SC where score <60 group by S# having count(*)>2)group by S#;
六:in ,between,like
select * from table1 where field1 like ’%value1%’ —like的语法很精妙,查资料!
select a,b,c from a where a IN (select d from b ) 或者: select a,b,c from a where a IN (1,2,3)
select * from table1 where a [not] in (‘值1’,’值2’,’值4’,’值6’)
select * from table1 where time between time1 and time2
select a,b,c, from table1 where a not between 数值1 and 数值2
SC(S#,C#,score) 成绩表
七:统计每门课程的学生选修人数(超过10人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列
,查询结果按人数降序排列,若人数相同,按课程号升序排列
select C# as 课程号,count() as 人数 from sc group by C# order by count() desc,c#;
八:查询前几
在SQL里。一般要选都是第一条都是
select top 2 a,b from table order by c.
在 ORACLE里
select * from (select a,b from table order by c) where rownum<=2; 先排序,再取前几位,注意优先级的问题。
注意1:TOP不能在Oracle中使用。
注意2:DISTINCT 必须放在第一个参数。

九:使用一个表进行联查时,必须要用到distinct,不然查询的结果是有两个一样的数据(不等号 a. != ; b. <>)
查询不同课程成绩相同的学生的学号、课程号、学生成绩
select distinct A.S#,a.score from SC A ,SC B where A.Score=B.Score and A.C# <>B.C# ;
十: CONVERT() 函数是把日期转换为新数据类型的通用函数。
CONVERT() 函数可以用不同的格式显示日期/时间数据。
十一: sql 查分数段人数
select count(case 分数字段 when 100 then 1 end) as [满分],
count(case when 分数字段 between 90 and 99 then 1 end) as[90-99分],
count(case when 分数字段 between 80 and 89 then 1 end) as[80-89分],
count(case when 分数字段 between 70 and 79 then 1 end) as[70-79分],
count(case when 分数字段<70 then 1 end) as[70分以下]
from 学生分数表

SELECT SC.C# as 课程ID, Cname as 课程名称

    ,SUM(CASE WHEN score BETWEEN 85 AND 100 THEN 1 ELSE 0 END) AS [100 - 85]

    ,SUM(CASE WHEN score BETWEEN 70 AND 85 THEN 1 ELSE 0 END) AS [85 - 70]

    ,SUM(CASE WHEN score BETWEEN 60 AND 70 THEN 1 ELSE 0 END) AS [70 - 60]

    ,SUM(CASE WHEN score < 60 THEN 1 ELSE 0 END) AS [60 -]

    FROM SC,Course where SC.C#=Course.C# GROUP BY SC.C#,Cname;

猜你喜欢

转载自blog.csdn.net/shiAndyuanfang/article/details/81331061