修改表結構
修改表名
alter table 表名 rename 新名
增加字段
alter table 表名 add 字段名 數據類型 約束
刪除字段
alter table 表名 drop 字段名
修改字段
alter table 表名 change 舊字段名 新字段名 數據類型 約束條件
修改字段順序
alter table 表名 add 字段名 數據類型 約束條件 first
#將該字段放在第一行
alter table 表名 add 字段名 數據類型 約束條件 after 字段名2
#將新添的字段放在字段名2后面
增刪改查
增
insert into upd01 select * from upd02;
insert into upd02(select id,name from upd03);
insert into upd03(id,name)(select * from upd01);
刪
delete from 表名;
delete from 表名 where 條件;
truncate table 表名;
改
update 表名 set 字段1="新值1",字段2="新值2" where 字段="值";
單表查
concat拼接
select concat("姓名:",字段1,"年齡",字段2) from 表名;
#拼接
select concat_ws(":",字段1,字段2) from employee;
#按照指定字符拼接
as設置別名
select 字段,字段名*12 as 別名 from 表名;
#設置別名
加減乘除
select 字段,字段名*12 from 表名;
#對字段值進行運算 加減乘除
distinct去重
select distinct 字段 from 表名;
#對結果去重
select distinct 字段,字段2 from 表名;
#對多個字段結果去重
case語句
select
(
case
when 條件1
then 結果1
when 條件2
then 結果2
end
)
from 表名;
where判斷
比較運算符
select emp_name,age from employee where post = "teacher" and age > 30;
between
select emp_name,age from employee where post = "teacher" and salary between 9000 and 10000;
#between 9000 and 10000 意思就是9000到10000
in
select emp_name,age from employee where post = "teacher" and salary in (9000,10000,30000);
#查看崗位是teacher且薪資是10000或9000或30000的員工姓名、年齡、薪資
not in
select emp_name,age from employee where post = "teacher" and salary not in (9000,10000,30000);
#查看崗位是teacher且薪資不是10000或9000或30000的員工姓名、年齡、薪資
是否為空
select emp_name from employee where post_comment is not null;
#查看崗位描述不為NULL的員工信息
模糊查詢
select emp_name,salary*12 from employee where post='teacher' and emp_name like 'jin%';
#查看崗位是teacher且名字是jin開頭的員工姓名、年薪
正則表達式
SELECT * FROM employee WHERE emp_name REGEXP '^ale';
#查詢emp_name以ale開頭的的所有數據
SELECT * FROM employee WHERE emp_name REGEXP 'on$';
#查詢emp_name以on結尾的所有數據
select * from employee where emp_name regexp "^jin.*[gn]$";
查看所有員工中名字是jin開頭,n或者g結果的員工信息
group by分組
group by 字段
#表示根據這個字段分組
select * from 表名 group by 字段名
-
group_concat
分組后顯示各組的數據
聚合函數
count()
- 總條數
select post,count(id) from employee group by post;
max()
- 最大值
select max(age) from employee;
min()
- 最小值
select min(age) from employee;
avg()
- 平均值
select avg(age) from employee;
sum()
- 求和
select sum(age) from employee;
having過濾
- 執行優先級從高到低:where > group by > having
- Where 發生在分組group by之前,因而Where中可以有任意字段,但是絕對不能使用聚合函數
- Having發生在分組group by之后,因而Having中可以使用分組的字段,無法直接取到其他字段,可以使用聚合函數
#查詢各崗位內包含的員工個數小於2的崗位名、崗位內包含員工名字、個數
select post ,group_concat(emp_name) as name ,count(id) as num from employee group by post having num < 2;
#查詢各崗位平均薪資大於10000的崗位名、平均工資
select post,avg(salary) as num from employee group by post having num > 10000;
#查詢各崗位平均薪資大於10000且小於20000的崗位名、平均工資
select post,avg(salary) as num from employee group by post having num > 10000 and num < 20000;
order by排序
單列排序
SELECT * FROM employee ORDER BY salary; #從小到大
SELECT * FROM employee ORDER BY salary ASC; #從小到大
SELECT * FROM employee ORDER BY salary DESC; #從大到小
多列排序
#查詢所有員工信息,先按照age升序排序,如果age相同則按照hire_date降序排序
select * from employee order by age,hire_date desc;
#查詢各崗位平均薪資大於10000的崗位名、平均工資,結果按平均薪資升序排列
select post,group_concat(emp_name) as name ,avg(salary) as num from employee group by post having num > 10000 order by num;
#查詢各崗位平均薪資大於10000的崗位名、平均工資,結果按平均薪資降序排列
select post,group_concat(emp_name) as name ,avg(salary) as num from employee group by post having num > 10000 order by num desc;
limit記錄數
select * from employee limit 3;
#顯示三行
select * from employee limit 5,5;
#從第5條開始數,數5條數據,也就是顯示6-10行的數據
select * from employee limit 10,5;
#從第10行開始數,數5條數據。
select執行順序
多表查詢
內連接
select * from employee a,department b where a.dep_id = b.id;
select * from employee a inner join department b on a.dep_id = b.id;
###兩個語句效果一樣
外連接之左連接
- left join 優先顯示左表的全部記錄,就是顯示左邊有右邊沒有的結果
select * from employee a left join department b on a.dep_id = b.id
外連接之右連接
- right join 優先顯示右表的全部記錄,就是顯示右邊有左邊沒有的結果
select * from employee a right join department b on a.dep_id = b.id
外連接之全連接
- union
select * from employee a left join department b on a.dep_id = b.id union select * from employee a right join department b on a.dep_id = b.id
- union與union all的區別:union會去掉相同的紀錄
子查詢
- 帶in關鍵字的子查詢
#查詢平均年齡在25歲以上的部門名
select id,name from department where id in (select dep_id from employee group by dep_id having avg(age) > 25);
#查看技術部員工姓名
select name from employee where dep_id in (select id from department where name='技術');
#查看不足1人的部門名(子查詢得到的是有人的部門id)
select name from department where id not in (select distinct dep_id from employee);
- 帶運算符的子查詢
#比較運算符:=、!=、>、>=、<、<=、<>
#查詢大於所有人平均年齡的員工名與年齡
mysql> select name,age from emp where age > (select avg(age) from emp);
#查詢大於部門內平均年齡的員工名、年齡
select a.name,a.age,b.avg_age from employee a,(select dep_id,avg(age) as avg_age from employee group by dep_id) as b where a.dep_id = b.dep_id and a.age > b.avg_age;
- 帶EXISTS關鍵字的子查詢
EXISTS關字鍵字表示存在。在使用EXISTS關鍵字時,內層查詢語句不返回查詢的記錄。而是返回一個真假值。True或False
當返回True時,外層查詢語句將進行查詢;當返回值為False時,外層查詢語句不進行查詢
#department表中存在dept_id=203,Ture
select * from employee where exists (select id from department where id=200);