select sex ,count(*) 人数 from stu group by sex;
select sex,avg(age) 平均年龄 from stu group by sex;
select address, count(*) 人数
from stu
where age < 45
group by address
having 人数 >= 2;
emp表
create table emp
(
id int not null
primary key,
ename varchar(50) null,
job_id int null,
mar int null,
joindata date null,
salary decimal(7, 2) null,
bonus decimal(7, 2) null,
dept_id int null,
constraint fk_emp_dept
foreign key (dept_id) references dept (id),
constraint fk_emp_job
foreign key (job_id) references job (id)
);
-- id 编号
-- ename 姓名
-- job_id 职业编号
-- mar 领导编号
-- joindat 入职日期
-- salary 薪水
-- bonus 奖金
-- dept_id 部门编号
dept表
create table dept
(
id int not null
primary key,
dname varchar(50) null,
loc varchar(50) null
);
-- id 部门编号
-- dname 部门名称
-- loc 部门地址
job表
create table job
(
id int not null
primary key,
jname varchar(20) null,
description varchar(50) null
);
-- id 工作编号
-- jname 职务
-- description 职务描述
salarygrade表(暂时用不到)
create table salarygrade
(
grade int not null
primary key,
losalary int null,
hisalary int null
);
select * from emp order by salary ;
select * from emp order by salary desc ;
select * from emp order by salary ,id desc ;
默认为升序
第三行先按salary查询,当salary相同时,再按id升序
select * from emp limit 0,5;
select * from emp limit 5;
select * from emp limit 5,4;
create user 'zjw'@'localhost' identified by '123456';
-- 刷新权限
flush privileges;
create user 'zjw'@'%' identified by '123456';
-- 刷新权限
flush privileges;
alter user 'zjw'@'localhost' identified with mysql_native_password by '1234';
drop user 'zjw'@'localhost';
show grants for 'zjw'@'localhost';
grant all on brand.* to 'zjw'@'localhost';
revoke all on brand.* from 'zjw'@'localhost';
select concat('hello',' MYSQL');
select lower('MYSQl');
select upper('hello');
-- 左填充
select lpad('pad', 8, '-');
-- 右填充
select rpad('pad', 8, '-');
select trim(' hello mysql ');
select substring('hello mysql',1,5);
select ceil(1.1);
select floor(1.9);
select mod(6,4);
select rand();
select round(3.156, 2);
生成一个六位的验证码
-- 返回小于一的正数 例如:0.041271461212329066
select rand();
-- 小数点前有 5位(十分位为 0的时候)或者 6位 例如:799453.843198208
select rand() * 1000000;
-- 保留一位小数
select round(rand() * 1000000, 0);
-- 不足六位的右填充为六位(左右填充都可以)
select rpad(round(rand() * 1000000, 0), 6, '0');
-- 返回小于一的正数 例如:0.041271461212329066
select rand();
-- 小数点后面保留 6位 例如:0.156836
select round(rand(), 6);
-- 放大1000000倍
select round(rand(), 6) * 1000000;
select curdate();
select curtime();
select now();
select day(curtime());
select month(curtime());
select year(curtime());
select date_add(curtime(), interval 70 day);
-- 与当前时间间隔70天
select date_add(curtime(), interval 70 month);
-- 与当前时间间隔70个月
select date_add(curtime(), interval 70 year);
-- 与当前时间间隔70年
select datediff('2021-10-12', '2002-12-01');
-- 6890
select ename, datediff(curdate(), joindata) as entrydate
from emp
order by entrydate desc;
select if(false, 'ok', 'error');
select if(true, 'ok', 'error');
-- expr1不为空,返回 '不是null'
select ifnull('不是null', 'Default');
-- expr1不为空,返回 ''(就是空白)
select ifnull('', 'Default');
-- expr1为空,返回 expr2
select ifnull(null, 'Default');
select ename,
(case job_id
when 1 then '高级员工'
when 2 then '中级员工'
when 3 then '普通员工'
else '实习生' end) as '等级'
from emp;
-- 注意不要把end漏掉
alter table emp
add constraint fk_emp_dept
foreign key (dept_id) references dept (id) on UPDATE cascade on DELETE cascade;
alter table emp
add constraint fk_emp_dept
foreign key (dept_id) references dept (id) on UPDATE set null on DELETE set null ;
select *
from emp,
dept
where emp.dept_id = dept.id;
select ename, dept_id
from emp e
inner join dept d on e.dept_id = d.id
select e.*, d.dname
from emp e
left join dept d on d.id = e.dept_id;
select e.*, d.*
from emp e
right outer join dept d on d.id = e.dept_id;
查询员工对应的领导
select a.ename, b.ename
from emp a,
emp b
where a.mar = b.id;
select * from emp where salary>10000
union all
select * from emp where bonus>5000;
select * from emp where salary>10000
union
select * from emp where bonus>5000;
select *
from emp
where dept_id =
(select id
from dept
where dname = '教研部');
select *
from emp
where joindata > (select joindata
from emp
where ename = '李逵');
列子查询(单列可多行)
查询在销售部或者财务部的员工信息
select *
from emp
where dept_id in (select id
from dept
where dname = '销售部'
or dname = '财务部');
select ename
from emp
where salary > all (select salary
from emp
where dept_id = (select id
from dept
where dname = '销售部'));
select ename
from emp
where salary > any (select salary
from emp
where dept_id = (select id
from dept
where dname = '销售部'));
select *
from emp
where (salary, mar) = (select salary, mar
from emp
where ename = '关羽'
);
select *
from emp
where (salary, mar) in (select salary, mar
from emp
where ename = '关羽'
or ename = '宋江'
);
select e.*, d.*
from (select *
from emp
where joindata > '2001-12-03'
) e
left join dept d on e.dept_id = d.id;
create table account
(
id int auto_increment
primary key,
name varchar(10) null,
money double(10, 2) null
);
-- 查询张三账户余额
select *
from account
where name = '张三';
-- 张三减去1000元
update account
set money=money - 1000
where name = '张三';
-- 李四加上1000元
update account
set money=money + 1000
where name = '李四';
-- 默认事务是自动提交的
select @@autocommit;
-- 把事务的提交方式设置为手动提交
set @@autocommit = 0;
-- 查询张三账户余额
select *
from account
where name = '张三';
-- 张三减去1000元
update account
set money=money - 1000
where name = '张三';
-- 李四加上1000元
update account
set money=money + 1000
where name = '李四';
-- 提交事务
commit;
-- 回滚事务,出现异常时执行
rollback;
-- 自动提交已经开启
set @@autocommit = 1;
-- 开启事务,只是限制本次操作为手动提交
start transaction;
-- 查询张三账户余额
select *
from account
where name = '张三';
-- 张三减去1000元
update account
set money=money - 1000
where name = '张三';
-- 李四加上1000元
update account
set money=money + 1000
where name = '李四';
-- 提交事务
commit;
-- 回滚事务,出现异常时执行
rollback;
隔离级别 | 脏读 | 不可重复读 | 幻读 |
---|---|---|---|
read uncommitted | √ | √ | √ |
read committed | × | √ | √ |
repeatable read | × | × | √ |
serializable | × | × | × |
-- 查看事务隔离级别,mysql默认为REPEATABLE-READ
select @@transaction_isolation;
-- 设置事务的隔离级别
set session transaction isolation level read committed;
事务隔离级别越高,数据越安全,但是性能越低