SQL--检索记录

1.检索所有行和列
select * from emp

2.筛选行(例子:查找部门编号为10的所有员工)
select * from emp where deptno = 10

3.查找满足多个查询条件的行(例:查出部门编号为10的所有员工、有业务提成的所有员工以及部门编号是20且工资低于2000美元的所有员工)
select * from emp where deptno = 10
or comm is not null
or sal <= 2000 and deptno = 20

4.筛选列(例:查看员工的名字、部门编号和工资)
selet ename,deptno,sal from emp

5.创建有意义的列明(例:将列ename为a,列sal为b)
select ename as a,sal as b from emp

6.在where句中引用别名列
select * from(select ename as a,sal as b from emp) x where a < 500

7.串联多列的值
select ename + ‘Works as A’ + job as msg
from emp
where deptno = 10

8.在Select语句里使用条件逻辑
select ename,sal
case when sal < = 2000 then ‘UNDERPAID’
when sal >= 4000 then ‘OVERPAID’
else ‘OK’
end as status
from emp

9.限定返回行数
select top 5* from emp

10.随机返回若干行记录
select top 5 ename, job
from emp
order by newid()

11.查找null值(不能使用=或!=)
select * from emp
where comm is null

12.把null转化为实际值(使用coalesce函数)
select coalesce(comm,0)
from emp

13.查找匹配项(结合使用LIKE运算符和SQL通配符%)
select ename,job
from emp
where deptno in (10,20)
and(ename like ‘%I%’ or job like ‘%ER’)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值