原创  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 )

发表于 @ 2008年09月08日 17:34:00 | 评论( loading... ) | 编辑| 举报| 收藏

旧一篇: JAVA文件操作类和文件夹的操作(转帖) | 新一篇:AJAX与java servlet结合使用的小例子

  • 发表评论
  • 评论内容:
  •  
Copyright © andycpp
Powered by CSDN Blog