sql用法
2015-08-28 11:46
281 查看
1.在where字句中使用别名。
//错误!!!!
//直接这样写是不行的,where字句是不认识别名的
select sal as salary, comm as commission
from emp
where salary < 5000
//正确的方法,使用一个子视图
select
from (
select sal as salary, comm as commission
from emp
) x
where salary < 5000
2.多字段合并查询
//将name和age这两个字段的内容合并成一个ageInfo字段显示输出
select concat(name,' age is ', age) as ageInfo from person;
3.新增加一列,内容由其他部分计算得出
//在查询结果中增加了一个status列
//根据年龄的大小,分别填写'too young'或'too old'或'OK'
select name,age,
case when age<20 then 'too young'
when age>30 then 'too old'
else 'OK'
end as status
from person;
4.随机返回有限的(非全部)查询结果
//order语句负责随机,limit语句负责限制显示数目
select * from person order by rand() limit 1;
5.将null值显示为其他值
//coalesce函数负责将null转化为其他值显示
select id,coalesce(name,'No Name'),age from person;
6.对查询结果进行多关键字排序
//部门编号为主关键字,升序;
//工资为次要关键字,降序
select empno,deptno,sal,ename,job
from emp
order by deptno, sal desc
7.如果某个字段不存在于另一个表中,找出他
1 select deptno
2 from dept
3 where deptno not in (select deptno from emp )
//错误!!!!
//直接这样写是不行的,where字句是不认识别名的
select sal as salary, comm as commission
from emp
where salary < 5000
//正确的方法,使用一个子视图
select
from (
select sal as salary, comm as commission
from emp
) x
where salary < 5000
2.多字段合并查询
//将name和age这两个字段的内容合并成一个ageInfo字段显示输出
select concat(name,' age is ', age) as ageInfo from person;
3.新增加一列,内容由其他部分计算得出
//在查询结果中增加了一个status列
//根据年龄的大小,分别填写'too young'或'too old'或'OK'
select name,age,
case when age<20 then 'too young'
when age>30 then 'too old'
else 'OK'
end as status
from person;
4.随机返回有限的(非全部)查询结果
//order语句负责随机,limit语句负责限制显示数目
select * from person order by rand() limit 1;
5.将null值显示为其他值
//coalesce函数负责将null转化为其他值显示
select id,coalesce(name,'No Name'),age from person;
6.对查询结果进行多关键字排序
//部门编号为主关键字,升序;
//工资为次要关键字,降序
select empno,deptno,sal,ename,job
from emp
order by deptno, sal desc
7.如果某个字段不存在于另一个表中,找出他
1 select deptno
2 from dept
3 where deptno not in (select deptno from emp )
相关文章推荐
- mysql如何查看超出多少天的记录数?
- Jedis使用总结【pipeline】【分布式的id生成器】【分布式锁【watch】【multi】】【redis分布式】
- MySql修改root密码、设置IP访问
- Redis的Java客户端Jedis的八种调用方式(事务、管道、分布式)介绍
- mysql 批量更新
- redis事务中的WATCH命令和基于CAS的乐观锁
- 浅谈sql中的in与not in,exists与not exists的区别
- 简单改写SQL达到优化目的
- oracle执行update时卡死问题的解决办法
- MongoDB跳出循环的两种方式
- MongoDB内存使用原理
- PL/SQL Developer,大小写转换
- Oracle 查询耗时 SQL
- oracle学习笔记 ---- 子查询
- mysql索引浅谈
- mysql连接字符
- tomcat memcached session 共享实现
- MongoDB用$group实现组合项去重
- 关于VBS连接MySQL与连接Excel
- oracle学习笔记 ---- 多表联查