【无标题】四类sql语句通用

select

select a from tableA where a=a1:

总是丢掉from。。

运算:

select a*3 b from tableA;

使用()来定义运算优先级。

别名

select a as xx from tableA;可以不加as,仅为增加可读性。

别名不可以中间有空格,

别名不可以有特殊符号,

别名不可以用关键字,

但是以上问题都可以被" "解决。

课后题:搜索empno,输出为员工号;搜索salary,输出为月薪;搜索salary,乘14后输出为14薪。

select empno 员工号,salary 月薪,salary*14 14薪 from emplyees;

distinct 去重复值;

select distinct a from tableA;

where

五种运算符。=\>\<\!=\<=\>=

select a from employees where a=a1;

select * from employees where hire_date<'2010-01-01'

// where < 居然可以比较data

课后题:提取表格中雇佣时间在2020年1月1日前的员工,输出他们的姓名、薪资、增加10%收入后薪资和入职时间;

select name,salary,salary*1.1 as "updated salary",hire_date from empolyees where hire_date<'2010-01-01'

and or not
and-且 :select * from empolyees where deptno=3 and salary>10000;
or-或 :select * from empolyees where deptno=3 or salary>10000;
and与or叠加:and优先级更高。

select * from employees where deptno=3 or salary>10000 and hire_date>'2020-01-01'

等同于:

select * from employees where deptno=3 or (salary>10000 and hire_date>'2020-01-01')
not与or叠加:

not (A or B)=not A and not B

课后练习:用2种语句写出符合条件的sql。一种用not,一种不用not

查询员工表中部门号不是3且工资大于15000的员工。

select * from employees where not (deptno=3 or salary<=15000);

select * from employees where deptno!=3 and salary>15000;

IN键匹配多个值:

select * from employees where deptno in (1,2,3)

等价于

select * from employees where deptno=1 or deptno=2 or deptno=3

not in:符合集合的部分都排除。

between指定范围:

select * from employees where hire_data>='2013-01-01' and hire_data<='2015-01-01'

等价于

select * from employees where hire_data between '2013-01-01' and '2015-01-01'

not between:区间范围内的排除;

课后练习:

查询工资在10000和15000之间的员工;

select * from employees where salary between 10000 and 15000;

like搜索匹配的字符串:

select * from employees where name like '%卫';

李%:搜索任意长度的李某,如李大,李世民,李文强。

%卫%:搜索任何含有卫的名字,不管是第一、中间、结尾。百分号可以为空。卫大、赵卫华、大卫。

select * from employees where email like '__a%';`

查询所有email邮箱第三位为a的email。前面的__只需要能够空出位置即可,内容不关注,有两个_,空出2个位置;后面的%匹配任意长度字符串,完全不关注。

select * from employees where email like '%@qq.com';`

查出使用qq邮箱的员工。

select * from employees where email like '____@%';`

查询邮箱前面使用了4个字母的员工,如qwer@qq.com

课后的练习:

查询email字段中第二个字母是h的gmail邮箱。

select * from employees where email like '_h%@gmail.com';

相关推荐
一 乐3 小时前
车辆管理|校园车辆信息|基于SprinBoot+vue的校园车辆管理系统(源码+数据库+文档)
java·前端·数据库·vue.js·论文·毕设·车辆管理
得物技术3 小时前
告别数据无序:得物数据研发与管理平台的破局之路
大数据·数据库·数据分析
EndingCoder4 小时前
Node.js 数据查询优化技巧
服务器·javascript·数据库·node.js·数据查询优化
TDengine (老段)4 小时前
TDengine 数学函数 SIGN 用户手册
大数据·数据库·sql·时序数据库·iot·tdengine·涛思数据
RestCloud5 小时前
Kingbase 与 ETL:如何实现金融级数据库的安全数据同步
数据库·数据安全·etl·数据处理·数据传输·数据同步·kingbase
Elastic 中国社区官方博客5 小时前
在 Elastic Observability 中,启用 TSDS 集成可节省高达 70% 的指标存储
大数据·运维·数据库·elasticsearch·搜索引擎·全文检索·时序数据库
Thepatterraining5 小时前
MySQL数据存储黑科技:Page布局、行存储与压缩算法全解密
数据库·mysql
ZhengEnCi6 小时前
SQL多表查询完全指南-从JOIN到复杂关联的数据整合利器
sql
wan5555cn6 小时前
中国启用WPS格式进行国际交流:政策分析与影响评估
数据库·人工智能·笔记·深度学习·算法·wps
惜分飞6 小时前
raid恢复之后数据库故障处理(ora-01200,ORA-26101,ORA-600)---惜分飞
数据库·sql·oracle·oracle恢复·raid恢复