dql 主要是对 数据库进行查询
主要学习 select 命令的使用
centos 7
mysql 7.5.43(yum安装)
创建一个表格
CREATE TABLE company.employee5(
id int primary key AUTO_INCREMENT not null,
name varchar(30) not null,
sex enum('male','female') default 'male' not null,
hire_date date not null,
post varchar(50) not null,
job_description varchar(100),
salary double(15,2) not null,
office int,
dep_id int
);
插入数据 方便之后查询
insert into company.employee5(name,sex,hire_date,post,job_description,salary,office,dep_id) values
('jack','male','20180202','instructor','teach',5000,501,100),
('tom','male','20180203','instructor','teach',5500,501,100),
('robin','male','20180202','instructor','teach',8000,501,100),
('alice','female','20180202','instructor','teach',7200,501,100),
('tianyun','male','20180202','hr','hrcc',600,502,101),
('harry','male','20180202','hr',NULL,6000,502,101),
('emma','female','20180206','sale','salecc',20000,503,102),
('christine','female','20180205','sale','salecc',2200,503,102),
('zhuzhu','male','20180205','sale',NULL,2200,503,102),
('gougou','male','20180205','sale','',2200,503,102);
这就是创建好之后的表格
这里查看了全部数据
select * from employee5;
# 根据要求查看数据
--查询工资小于五千 这里只看的是名字
select name from company.employee5 where salary <5000;
--如果要看工资 则需要把工资加上
select salary,name from company.employee5 where salary <5000;
-- > < >= <= 1=
# 运算查询
SELECT name,salary *14 from company.employee5;
--查看 十四个月的工资
SELECT name,salary*14 as sum_salary from company.employee5;
SELECT name as king,salary*14 as sum_salary from company.employee5;
# 查询所有部门
select dep_id from company.employee5;
-- 去重(避免重复)
select distinct dep_id from company.employee5; --去重
# 拼接两个字段
select name,post from company.employee5;
select concat(name,'_is_',post) from company.employee5;
这里用了五种方法进行查询
修改一个员工时间为2017
update company.employee5 set {new date} where {old date}
update company.employee5 set hire_date="2017-3-15" where name='jack';
--1、统配符查询
SELECT * FROM employee5 WHERE hire_date like "2017%";
--2017% 中 % 这里代表匹配所有的意思
--2、位置查询(lefi right)
SELECT * FROM employee5 WHERE LEFT(hire_date,4)=2017;
--LEFI(hire_date,4) 左边四位
--RIGHT(hire_date,4) 右边四位
--3、 时间范围
SELECT * FROM employee5 WHERE hire_date >"2016-12-31" and hire_date < "2018-01-01";
-- 4、between查询
SELECT * FROM employee5 WHERE hire_date BETWEEN "2016-12-31" and "2018-01-01";
-- 5、正则
SELECT * FROM employee5 WHERE hire_date REGEXP "2017"
1、等价于没有任何值、是未知数。
2、NULL与0、空字符串、空格都不同,NULL没有分配存储空间。
3、对空值做加、减、乘、除等运算操作,结果仍为空。
4、比较时使用关键字用“is null”和“is not null”。
5、排序时比其他数据都小,所以NULL值总是排在最前。
# 查询 NULL
select * from company.employee5 where job_description is NULL;
# 查询 不是空的
select * from company.employee5 where job_description is NOT NULL;
#空 字符串 不等于 NULL
select name.post from company.employee5 where id in (1,3,5);
select * from company.employee5 order by salary desc;
-- desc 降序
select * from company.employee5 order by salary;
-- asc 升序 (如果不写 默认是升序asc)
select post from company.employee5 group by post;
查看包含20 的内容
SELECT * from company.employee5 WHERE salary like '%20%';
在这几个数据中 都包含20
#查询有这些数据722222 7222222222
SELECT * FROM employee5 WHERE salary REGEXP '72+';
#查询 开头是ali
SELECT * FROM employee5 WHERE name REGEXP '^ali';
#查询结尾是yun
SELECT * FROM employee5 WHERE name REGEXP 'yun$';
#查询 m 出现的次数 出现了1-8次
SELECT * FROM employee5 WHERE name REGEXP 'm{1,8}';
-- 最多出现八次
SELECT * FROM employee5 WHERE name REGEXP 'm{,8}';
-- 最多出现一次
SELECT * FROM employee5 WHERE name REGEXP 'm{1,}';
count() --统计数量 查看一共有多少条数据
select count(*) from company.employee5;
max() --查询最大哪个值
SELECT name,salary FROM company.employee5 WHERE salary = (SELECT MAX(salary) FROM company.employee5);
min() --查询最小值
SELECT name,salary FROM company.employee5 WHERE salary = (SELECT MIN(salary) FROM company.employee5);
avg() --平均值
SELECT avg(salary) from company.employee5;
-- 某个部门的平均工资
database() --显示当前数据库
select database();
user() --查看当权用户
SELECT user();
now() --打印当前时间
SELECT now();
sum() --对某字段数据进行求和
SELECT SUM(salary) AS total_amount FROM company.employee5;
password() --用来设置加密后的密码
目前官方已经不推荐使用了(好吧 我承认这里我走神了 大家知道有这个东西就可以了)
🌸完结 撒花 🌸
🌸择一人 忠一生 很简单 很幸福🌸
🌸回静明月 潇洒一生🌸