sql 不常用但管用的关键词

1、any和all

  

--any表示任意一个,all表示所有的。
--如果有张学生记录表student中有一个属性组为age
--现在要查找年龄在某个区间上的学生记录就有如下操作
--1、查找年龄比15、16、22、21、17、18、19中任意一个都小的学生记录就有如下代码:

select * from student where age<any(15,16,22,21,17,18,19)

--2、查找年龄比15、16、22、21、17、18、19中任意一个都大的学生记录就有如下代码:

select * from student where age>any(15,16,22,21,17,18,19)

--/*这里用any 和all是等效的*/用all是大于所有的意思 
--用all就改为:

where age>all(15,16,22,21,17,18,19) 

 2、union 和 union all

    

--再两条查询语句的结果集相同是使用  
--union 和并查询结果集并去掉重复结果  
--union  all 合并不去重复  
  select * from a where id = 3 union select * from a  
  select * from a where id = 3 union all select * from a  
      
  结果集可能不一样  

 

 3、decode 和 case when 

   
--decode相当于if-else
select 
  --当T.gx_dept_name的值为“香蜜湖公交所”时截取前三个字符 
  decode(T.gx_dept_name,'香蜜湖公交所',substr(T.gx_dept_name, 0, 3),substr(T.gx_dept_name, 0, 2)) as dept_name,  
  --当T.num为空时值为0当不为空时取T.num的值 
  decode(T.num, null, 0, T.num) as num from ......

--case相当于if...else if...else if...else
--当T.num为空时值为0当不为空时取T.num的值 
  case when T.num=null then 0 elseT.num end as num 
 

1、any和all

  

--any表示任意一个,all表示所有的。
--如果有张学生记录表student中有一个属性组为age
--现在要查找年龄在某个区间上的学生记录就有如下操作
--1、查找年龄比15、16、22、21、17、18、19中任意一个都小的学生记录就有如下代码:

select * from student where age<any(15,16,22,21,17,18,19)

--2、查找年龄比15、16、22、21、17、18、19中任意一个都大的学生记录就有如下代码:

select * from student where age>any(15,16,22,21,17,18,19)

--/*这里用any 和all是等效的*/用all是大于所有的意思 
--用all就改为:

where age>all(15,16,22,21,17,18,19) 

 2、union 和 union all

    

--再两条查询语句的结果集相同是使用  
--union 和并查询结果集并去掉重复结果  
--union  all 合并不去重复  
  select * from a where id = 3 union select * from a  
  select * from a where id = 3 union all select * from a  
      
  结果集可能不一样  

 

 3、decode 和 case when 

   
--decode相当于if-else
select 
  --当T.gx_dept_name的值为“香蜜湖公交所”时截取前三个字符 
  decode(T.gx_dept_name,'香蜜湖公交所',substr(T.gx_dept_name, 0, 3),substr(T.gx_dept_name, 0, 2)) as dept_name,  
  --当T.num为空时值为0当不为空时取T.num的值 
  decode(T.num, null, 0, T.num) as num from ......

--case相当于if...else if...else if...else
--当T.num为空时值为0当不为空时取T.num的值 
  case when T.num=null then 0 elseT.num end as num 
 

猜你喜欢

转载自x125858805.iteye.com/blog/1810908