1. 程式人生 > 其它 >MySQL資料庫命令大全

MySQL資料庫命令大全

--資料庫操作前的準備
-- 建立資料庫
-- create database python_test_1 charset=utf8;

-- 使用資料庫
-- use python_test_1;

-- students表
-- create table students(
-- id int unsigned primary key auto_increment not null,
-- name varchar(20) default '',
-- age tinyint unsigned default 0,
-- height decimal(5,2),
-- gender enum('男','女','中性','保密') default '保密',
-- cls_id int unsigned default 0,
-- is_delete bit default 0
-- );

-- classes表
-- create table classes (
-- id int unsigned auto_increment primary key not null,
-- name varchar(30) not null
-- );


-- 查詢練習
-- 查詢所有欄位
-- select * from 表名;
select * from students;


-- 查詢指定欄位
-- select 列1,列2,... from 表名;
select name,age from students;


-- 使用 as 給欄位起別名
-- select 欄位 as 名字.... from 表名;
select name as '姓名',age from students;

-- select 表名.欄位 .... from 表名;
select students.name from students;

-- 可以通過 as 給表起別名
-- select 別名.欄位 .... from 表名 as 別名;
select * from students as s;

select s.name from students as s;


-- 消除重複行(查性別)

-- distinct 欄位 不要記有個印象
select distinct gender from students;


> < >= <= != <>
-- 條件查詢
-- 比較運算子
-- select .... from 表名 where .....
-- >
-- 查詢年紀大於18歲的資訊
select * from students where age > 18;


-- <
-- 查詢年紀小於18歲的資訊
select * from students where age < 18;

-- >=
-- <=
-- 查詢小於等於18歲的資訊
select * from students where age <= 18;

-- =
-- 查詢年齡為18歲的所有學生的名字
select * from students where age = 18;

-- != 或者 <>
-- 查詢年齡不為18歲的所有學生的名字
select * from students where age != 18;
-- select * from students where age <> 18;
and or not
-- 邏輯運算子
-- and
-- 18和28之間的所以學生資訊
select * from students where age > 18 and age < 28;


-- 18歲以上的女性
select * from students where age > 18 and gender = '女';


-- or
-- 18以上或者身高高過180(包含)以上
select * from students where age > 18 or height >= 180;

-- not
-- 不在 18歲以上的女性 這個範圍內的資訊
-- select * from students where not (age>18 and gender=2);
select * from students where not age > 18 and gender= "女";

select * from students where not (age > 18 and gender = "女");

like % _
-- 模糊查詢(where name like 要查詢的資料)
-- like
-- % 替換任意個
-- _ 替換1個
-- 查詢姓名中 以 "小" 開始的名字
select * from students where name like '小%';

-- 查詢姓名中 有 "小" 所有的名字
select * from students where name like '%小%';

-- 查詢有2個字的名字
select * from students where name like '__';


-- 查詢有3個字的名字
select * from students where name like '___';

-- 查詢至少有2個字的名字
select * from students where name like '__%';

select * from students where name not like "__";


-- 範圍查詢
-- in (1, 3, 8)表示在一個非連續的範圍內
-- 查詢 年齡為18或34的姓名
select * from students where age = 18 or age = 34 ;
select * from students where age in (18,34);

-- not in 不非連續的範圍之內
-- 年齡不是 18或34歲的資訊
select * from students where age not in(18,34);


-- between ... and ...表示在一個連續的範圍內
-- 查詢 年齡在18到34之間的的資訊
select * from students where age > 18 and age < 34;
-- between xxx and xxx
select * from students where age between 18 and 34; --between...and...這是包含兩端的資料


-- not between ... and ...表示不在一個連續的範圍內
-- 查詢 年齡不在18到34之間的的資訊

select * from students where age not between 18 and 34;

-- 空判斷
-- 判空is null
-- 查詢身高為空的資訊
select * from students where height is null;


-- 判非空is not null
select * from students where height is not null;

order by 欄位 asc,desc

-- 排序
-- order by 欄位
-- asc
-- asc從小到大排列,即升序
-- desc
-- desc從大到小排序,即降序
-- 查詢年齡在18到34歲之間的男性,按照年齡從小到大到排序
select * from students where (age between 18 and 34) and gender='男' order by age asc;


-- 查詢年齡在18到34歲之間的女性,身高從高到矮排序
select * from students where (age between 18 and 34) and gender ='女' order by height desc;

-- order by 多個欄位
-- 查詢年齡在18到34歲之間的女性,身高從高到矮排序, 如果身高相同的情況下按照年齡從小到大排序
select * from students where (age between 18 and 34) and gender = '女' order by height desc,age asc;


-- 如果年齡也相同那麼按照id從大到小排序
select * from students where (age between 18 and 34) and gender ='女' order by height desc,age asc,id desc;
-- 排序有優先順序,第一個主排序,後面是次排序,在保證主排序不變的情況下,能排就排,不排就算了


-- 聚合函式
-- 總數
-- count
-- 查詢男性有多少人 count(欄位) 要注意如果值有null那麼不會進行計算
select count(*) from students where gender='男';

-- 最大值
-- max
-- 查詢最大的年齡
select max(age) from students;

-- 查詢女性的最高 身高
select max(height) from students where gender ='女';


-- 最小值
-- min
select min(age) from students ;


-- 求和
-- sum
-- 計算所有人的年齡總和
select sum(age) from students;


-- 平均值
-- avg
-- 計算平均年齡
select avg(age) from students;


-- 計算平均年齡 sum(age)/count(*)
select sum(age)/count(*) from students;


-- 四捨五入 round(123.23 , 1) 保留1位小數
-- 計算所有人的平均年齡,保留2位小數
select round (avg(age),2) from students;


-- 計算男性的平均身高 保留2位小數
select round(avg(height),2) from students where gender='男';
select avg(height) from students where gender = '男';


-- 分組

-- group by
-- 按照性別分組,查詢所有的性別
-- select 分組欄位 from 表名 group by 分組欄位;
select gender from students group by gender;

select 分組欄位 from 表名 group by 分組欄位;


-- 計算每種性別中的人數
select gender,count(*) from students group by gender;

-- group_concat(...)
-- 查詢同種性別中的姓名
select gender,group_concat(name) from students group by gender;



-- 查詢每組性別的平均年齡
select gender,avg(age) from students group by gender;


-- select * from students where
-- group by xxx having having用在分組條件

-- having(注意having和group by 連用 having後通常也要跟 聚合函式)
-- 查詢平均年齡超過30歲的性別,以及姓名
select gender ,avg(age) from students group by gender having avg(age) > 30;


-- 查詢每種性別中的人數多於2個的資訊
select gender,count(*) from students group by gender having count(*) > 2;

-- with rollup 彙總的作用(瞭解)
--select gender,count(*) from students group by gender with rollup;
select gender,count(*) from students group by gender with rollup having count(*) >2;


--按性別分組,平均身高大160的女性組的名字
select gender,avg(height),group_concat(name) from students group by gender having avg(height) > 160 and gender='女';


-- limit 起始位置,個數, 這個一定要放在最後
-- 分頁
-- limit start, count
-- limit 放在最後面(注意)

起始位置 = (頁數-1)*每頁的個數

-- 限制查詢出來的資料個數
-- 查詢前5個數據
select * from students limit 0,5;


-- 每頁顯示2個,第1個頁面
select * from students limit 0,2;

-- 每頁顯示2個,第2個頁面
select * from students limit 2,2;

-- 每頁顯示2個,第3個頁面
select * from students limit 4,2;

-- 每頁顯示2個,第4個頁面
select * from students limit 6,2;

-- 每頁顯示2個,顯示第6頁的資訊, 按照年齡從小到大排序
select * from students order by age asc limit 6,2;
-- 如果重新排序了,那麼會顯示第一頁



-- 連線查詢
-- inner join ... on
-- select ... from 表A inner join 表B;
-- 查詢 有能夠對應班級的學生以及班級資訊
select * from students inner join classes on students.cls_id = classes.id;


-- 按照要求顯示姓名、班級
select students.name,classes.name from students inner join classes on students.cls_id = classes.id;


-- 給資料表起名字
select s.name,c.name from students as s inner join classes as c on s.cls_id = c.id;


-- 查詢 有能夠對應班級的學生以及班級資訊,顯示學生的所有資訊 students.*,只顯示班級名稱 classes.name.
select students.* ,classes.name from students inner join classes on students.cls_id = classes.id;


-- 在以上的查詢中,將班級名顯示在第1列
select classes.name,students.* from students inner join classes on students.cls_id = classes.id;

-- 查詢 有能夠對應班級的學生以及班級資訊, 按照班級名進行排序
select classes.name,students.* from students inner join classes on students.cls_id = classes.id order by classes.name asc;



-- 當時同一個班級的時候,按照學生的id進行從小到大排序
select classes.name,students.* from students inner join classes on students.cls_id = classes.id order by classes.name asc,students.id asc;

#如果是group by 條件使用having
#如果是inner join條件使用on
#其他都用where

-- left join
-- 查詢每位學生對應的班級資訊
select * from students left join classes on students.cls_id = classes.id;
左邊的表不管在右邊的表中是否找到資料,都顯示


-- 查詢沒有對應班級資訊的學生
select * from students left join classes on students.cls_id = classes.id where classes.name is null;



-- right join on
-- 將資料表名字互換位置,用left join完成

select * from students right join classes on students.cls_id = classes.id;

select * from classes right join students on students.cls_id = classes.id;

-- 子查詢
-- 標量子查詢: 子查詢返回的結果是一個數據(一行一列)
-- 列子查詢: 返回的結果是一列(一列多行)
-- 行子查詢: 返回的結果是一行(一行多列)

-- 查詢出高於平均身高的資訊(height)
select avg(height) from students;

select * from students where height > 172;

select * from students where height > (select avg(height) from students);

-- 查詢學生的班級號能夠對應的 學生名字
select * from students where cls_id in (1,2);

select id from classes;

select * from students where cls_id in (select id from classes);
省市區三級聯動

--資料操作前的準備
--建立資料庫表
create table areas(
aid int primary key,
atitle varchar(20),
pid int
);
--從sql檔案中匯入資料
-- source 具體地址/areas.sql;
source areas.sql;


--查詢一共有多少個省
select * from areas where pid is null;

--例1:查詢省的名稱為“山西省”的所有城市
select aid from areas where atitle = '山西省';
select * from areas where pid = (select aid from areas where atitle = '山西省');


select * from areas as a1 inner join areas as a2 on a1.pid = a2.aid where a2.atitle='山西省';

--例2:查詢市的名稱為“廣州市”的所有區縣
select * from areas where pid = (select aid from areas where atitle = '廣州市');

select * from areas as a1 inner join areas as a2 on a1.pid = a2.aid where a2.atitle='廣州市';