SQL语句学习(不断更新中。。。。。。)

1.在where字句中使用别名。
  1. //错误!!!!
  2. //直接这样写是不行的,where字句是不认识别名的 
  3. select sal as salary, comm as commission
  4.    from emp
  5.   where salary < 5000
  6. //正确的方法,使用一个子视图
  7. select  
  8.  from  (
  9.    select sal as salary, comm as commission
  10.    from emp
  11.         ) x
  12.   where salary < 5000
2.多字段合并查询
  1. //将name和age这两个字段的内容合并成一个ageInfo字段显示输出
  2. select concat(name,' age is ', age) as ageInfo from person;
3.新增加一列,内容由其他部分计算得出
  1.  //在查询结果中增加了一个status列
  2.  //根据年龄的大小,分别填写'too young'或'too old'或'OK'
  3.  select name,age,
  4.  case when age<20 then 'too young'
  5.  when age>30 then 'too old'
  6.  else 'OK'
  7.  end as status
  8.  from person;
4.随机返回有限的(非全部)查询结果
  1. //order语句负责随机,limit语句负责限制显示数目
  2. select * from person order by rand() limit 1;
5.将null值显示为其他值
  1. //coalesce函数负责将null转化为其他值显示
  2. select id,coalesce(name,'No Name'),age from person;
6.对查询结果进行多关键字排序
  1. //部门编号为主关键字,升序;
  2. //工资为次要关键字,降序
  3. select empno,deptno,sal,ename,job
  4. from emp
  5. order by deptno, sal desc
7.如果某个字段不存在于另一个表中,找出他
  1.         1 select deptno
  2.         2   from dept
  3.         3  where deptno not in (select deptno from emp )

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值