2019.3.25 SQL语句(进阶篇1)

运算符

基本的加减乘除取余都可以在SQL中使用
新建Employee1表并添加数据

create table Employee1
(eid int primary key auto_increment,
name varchar(20),
age int,
salary int,
depart varchar(50),
workage int);

insert into employee1
values (null,'张三',30,10000,'研发部',3),
(null,'李四',22,5000,'市场部',2),
(null,'王五',29,6000,'营销部',3),
(null,'赵六',28,4000,'营销部',3),
(null,'钱七',26,12000,'研发部',4),
(null,'张四',32,9000,'人事部',4),
(null,'张五',22,4500,'策划部',1),
(null,'张六',23,5000,'市场部',2),
(null,'李三',19,3000,'研发部',1),
(null,'李五',26,6000,'人事部',2),
(null,'王三',42,18000,'研发部',20),
(null,'王四',31,13000,'人事部',9),
(null,'王六',27,7000,'人事部',5),
(null,'赵三',23,3500,'市场部',2),
(null,'赵四',36,11000,'策划部',11),
(null,'赵五',25,4000,'人事部',2),
(null,'钱三',24,6000,'研发部',2),
(null,'钱四',44,14000,'策划部',4),
(null,'钱五',32,9000,'市场部',8),
(null,'钱六',50,2000,'研发部',30);

as:修改列名/表名(可隐藏)

select salary/2 as result, salary s
from Employee1;

图太长了截一半了

比较运算符:= > < >= <=

is null/is not nul :l判断是否为空

select *
from Employee1
where depart is not null;

=/<=>:判断两个值是否相等

<=>可以判断两个空值相等
select null <=> null ;

between...and....:在...之间

select *
from Employee1
where salary between 6000 and 12000;

in/not in:是否存在

select *
from Employee1
where depart in ('市场部','人事部');

like:模糊查询

select *
from Employee1
where name like '张%'; //%后面只是占位 不算位数 代表一串字符

select *
from Employee1
where name like '张_'; //_代表一个字符

因为我的表里只有两个字的名字 所以_和%查出的东西是一样的

逻辑运算符

and &&
or ||
not !
xor 异或

& 位与
| 位或
^ 位异或
<< 位左移

位右移
-位取反


函数

常用函数

CONCAT(): 字符串拼接

select CONCAT('我是',name,depart)
from Employee1;

LOWER/UPPER(): 大小写改变

select LOWER('ASERDFTY');
select UPPER('qwertyyu');


INSERT():字符串替换

select INSERT('这是一个字符串',3,5,'试试'); //从第3个开始截5个

SUBSTRING(): 截取子字符串

select SUBSTRING('这是一个字符串',3,5);

CEIL():向上取整

select CEIL(3.3);

FLOOR():向下取整

select FLOOR(3.3);

rand():生成随机数

select rand();

聚合函数

AVG():取平均值

select AVG(salary)
from Employee1;

SUM():求和

select SUM(salary)
from Employee1;

MAX()/MIN():求最大值最小值

select MAX(salary)
from employee1;

select MIN(salary)
from employee1;

COUNT():求数量

select salary,COUNT(*)
from Employee1;

posted @ 2019-03-25 19:26  仲夏今天也在写前端  阅读(258)  评论(0编辑  收藏  举报