整理常用SQL語句(二)
阿新 • • 發佈:2020-07-15
修改欄位
alter table table_name modify propName propType; # 修改欄位的型別
alter table table_name change pnameOld pnameNew pTypeOld; # 修改欄位名
alter table table_name change pnameOld pnameNew PTypeNew; # 同時修改欄位名和型別
查詢
給欄位取別名
-- 別名
select name as 姓名,gender as 性別 from student_table;
-- 或者
select name 姓名, gender 性別 from student_table;
給表取別名
select * from student_table as stu;
去重 distinct
selet distinct gender from student_table;
-- 只顯示男、女
where 條件
select * from student_table where gender = '男';
select name, age from student_table where gender = '男’;
-- '*' 處控制列, where 控制行
比較運算子
=
<
<=
=
!= 、 <>
-- 查詢小於30 歲的
select name from student_table where age <= 30;
邏輯運算
and
or
not
模糊查詢 like
% 代表任意字元
_ 代表一個任意字元
-- 以孫開頭的學生
select * from student_table where name like '孫%';
-- 以姓孫且名字兩位的
select * from student_table where name like '孫_';
-- 以喬結尾
select * from sudent_table where name like '%喬';
-- 名字中有白
select * from student_table where name like '%白%';
-- 名字兩個字
select * from student_table where name like '__';
範圍查詢 in 、between... and...(閉區間)
-- 老家是北京、上海
select * from student_table where hometown = '北京' or homtown = '上海';
select * from student_table where hometown in ('北京', '上海');
-- 年齡在25到30
select * from student_table where ages >=25 and ages <= 30;
select * form student_table where ages between 25 and 30;
空判斷: is null
-- 查詢身份證為空的學生
select * from student_table where id_card is null;
修改
update student_table set field1 = value where condition;
update student_table set class = '1班' where name like '孫%';
刪除
delete from tb_name where conditions;
delete from student_table where class = '1班';
排序 order by
order by 欄位 asc(從小到大,預設)\ desc(從大到小)
-- 年齡從小到大
select * from student_table order by age;
-- 年齡從小到大,年齡相同按學號
select * from student_table order by age , id;
-- 男學生從大到小
select * from student_table where gender = '' order by age desc;
聚合函式
count()
-- 查詢學生總數
select count(*) from student_table;
-- 查詢班級數
select count(distinct class) from student_table;
-- 女生數量
select count(*) from student_table where gender = '';
max()
-- 年齡最大
select max(age) from student_table;
-- 年齡最大的女
select max(age) from student_table where gender = '0';
-- !!! 聚合函式不能用到where 後面的條件
min()
sum()
-- 年齡總和
select sum(age) from student_table;
avg()
-- 平均年齡
select avg(age) from student_table;
-- avg 自動忽略null,null 作為字母參與計算
-- 查詢最大年齡、和最小年齡
select max(age) 最大年齡, min(age) 最小年齡 from student_table;
資料分組 group by
-- 分別查詢男女同學的數量
select count(*) from studens group by gender;
select sex, count(*) from studens group by gender;
-- !!! gourp by 是配合 聚合函式使用的
-- 1班男女數量
select sex, count(*) from student_table where class = '' group by gender;
select count(*) from st_table where conditions group by field order by field;
having
-- 查詢男生總數
-- 篩選 聚合
select count(*) from student_table where gender='';
-- 分組 聚合 篩選
select count(*) from student_table group by gender having gender = '';
-- !!! having 總是在group by 後面
-- !!! having 後面可以用聚合函式
-- 求班級大於3人的名字
select class from student_table group by having count(*) > 3;
-- where 是對原始資料進行處理,後面不能用聚合函式
-- having 是對篩選出來的資料進行處理 ,可以用聚合函式
分頁 limit start, num
select * from table_name limit 0, 3;
select * from tbale_name limit 3; # default start = 0
-- limit 總是出現在sql 語句的最後;
-- 查詢年齡最大學生姓名
select name from student_table order by age desc limit 1;
-- 求第n頁資料
select name from student_table limit (n-1)*m, m;