--數(shù)據(jù)庫(kù)操作前的準(zhǔn)備
-- 創(chuàng)建數(shù)據(jù)庫(kù)
-- create database python_test_1 charset=utf8;
create database python_test_1 charset=utf8;
-- 使用數(shù)據(jù)庫(kù)
-- use python_test_1;
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
-- );
-- 查詢練習(xí)
-- 查詢所有字段
-- select * from 表名;
select * from students;
-- 查詢指定字段
-- select 列1,列2,... from 表名;
select name, age from students;
-- 使用 as 給字段起別名
-- select 字段 as 名字.... from 表名;
select name, age from students as "名字","年齡"
select name as "名字",age as"年齡" from students;
-- select 表名.字段 .... from 表名;
select students.name from students;
-- 可以通過(guò) as 給表起別名
-- select 別名.字段 .... from 表名 as 別名;
select "學(xué)生表".name, "學(xué)生表".age from students as "學(xué)生表";
-- 消除重復(fù)行(查性別)
-- distinct 字段
select distinct(gender) from students;
-- 條件查詢
-- 比較運(yùn)算符
-- select .... from 表名 where .....
-- >
-- 查詢年紀(jì)大于18歲的信息
select * from students where age >18;
-- <
-- 查詢年紀(jì)小于18歲的信息
select * from students where age <18;
-- >=
-- <=
-- 查詢小于或者等于18歲的信息
-- =
-- 查詢年齡為18歲的所有學(xué)生的名字
select name from students where age =18;
-- != 或者 <>
-- 查詢年齡不為18歲的所有學(xué)生的名字
select name from students where age =18;
-- 邏輯運(yùn)算符
-- and
-- 18和28之間的所以學(xué)生信息
select * from students where age >=18 and age <=28;
-- 18歲以上的女性
select * from students where age >=18 and gender = '女';
-- or
-- 18以上或者身高高過(guò)180(包含)以上
select * from students where age >18 or height >180;
-- not
-- 不在 18歲以上的女性 這個(gè)范圍內(nèi)的信息
-- select * from students where not (age>18 and gender=2);
select * from students where not (age>18 and gender = "女");
-- 模糊查詢(where name like 要查詢的數(shù)據(jù))
-- like
-- % 替換任意個(gè)
-- _ 替換1個(gè)
-- 查詢姓名中 以 "小" 開(kāi)始的名字
select * from students where name like "小%";
-- 查詢姓名中 有 "小" 所有的名字
select * from students where name like"%小%";
-- 查詢有2個(gè)字的名字
select* from students where name like "__";
-- 查詢有3個(gè)字的名字
select * from students where name like "___";
-- 查詢至少有2個(gè)字的名字
select * from students where name like "__%";
-- 范圍查詢
-- in (1, 3, 8)表示在一個(gè)非連續(xù)的范圍內(nèi)
-- 查詢 年齡為18或34的姓名
select * from students where age in (18,34);
-- not in 不非連續(xù)的范圍之內(nèi)
-- 年齡不是 18或34歲的信息
select * from students where age not in (18,34);
-- between ... and ...表示在一個(gè)連續(xù)的范圍內(nèi)
-- 查詢 年齡在18到34之間的的信息
select * from students where age between 18 and 34;
-- not between ... and ...表示不在一個(gè)連續(xù)的范圍內(nèi)
-- 查詢 年齡不在18到34之間的的信息
select * from students where not (age 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
-- 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 >=18 and age <=34 and gender = '女' order by height desc;
-- order by 多個(gè)字段
-- 查詢年齡在18到34歲之間的女性,身高從高到矮排序, 如果身高相同的情況下按照年齡從小到大排序
select * from students where age >18 and age <= 34 and gender ="女" order by height desc, age asc;
-- 如果年齡也相同那么按照id從大到小排序
select * from students where age >18 and age <= 34 and gender ="女" order by height desc, age asc,id desc;
-- 聚合函數(shù)
-- 總數(shù)
-- count
-- 查詢男性有多少人
select count(*) from students where gender = "男";
-- 最大值
-- max
-- 查詢最大的年齡
select max(age) from students;
-- 查詢女性的最高 身高
select max(height) from students where gender = "女";
-- 最小值
-- min
select max(height) from students where gender = "男";
-- 求和
-- sum
-- 計(jì)算所有人的年齡總和
select sum(age) from students;
-- 平均值
-- avg
-- 計(jì)算平均年齡
select avg(age) from students;
-- 計(jì)算平均年齡 sum(age)/count(*)
select sum(age)/count(*) from students;
-- 四舍五入 round(123.23 , 1) 保留1位小數(shù)
-- 計(jì)算所有人的平均年齡,保留2位小數(shù)
select round(avg(age),2) from students;
-- 計(jì)算男性的平均身高 保留2位小數(shù)
select round(avg(age),2) from students where gender = '男';
-- 分組
-- group by
-- 按照性別分組,查詢所有的性別
select gender from students group by gender;
-- 計(jì)算每種性別中的人數(shù)
select count(*) from students group by gender;
-- group_concat(...)
-- 查詢同種性別中的姓名
select group_concat(name) from students group by gender;
-- 查詢每組性別的平均年齡
select avg(age) from students group by gender;
-- having(注意having和group by 連用 having后通常也要跟 聚合函數(shù))
-- 查詢平均年齡超過(guò)30歲的性別,以及姓名
select name, gender from students group by age having age >30;
-- 查詢每種性別中的人數(shù)多于2個(gè)的信息
select * from students group by gender having count(gender)>2;
-- with rollup 匯總的作用(了解)
--select gender,count(*) from students group by gender with rollup;
select gender, count(*) from students group by gender with rollup;
-- 分頁(yè)
-- limit start, count
-- limit 放在最后面(注意)
-- 限制查詢出來(lái)的數(shù)據(jù)個(gè)數(shù)
-- 查詢前5個(gè)數(shù)據(jù)
-- 每頁(yè)顯示2個(gè),第1個(gè)頁(yè)面
select * from students limit 0, 2;
-- 每頁(yè)顯示2個(gè),第2個(gè)頁(yè)面
select * from students limit 2, 2;
-- 每頁(yè)顯示2個(gè),第3個(gè)頁(yè)面
select * from students limit 4, 2;
-- 每頁(yè)顯示2個(gè),第4個(gè)頁(yè)面
select * from students limit 6, 2;
-- 每頁(yè)顯示2個(gè),顯示第6頁(yè)的信息, 按照年齡從小到大排序
select * from students order by age asc limit 6, 2;
-- 連接查詢
-- inner join ... on
-- select ... from 表A inner join 表B;
-- 查詢 有能夠?qū)?yīng)班級(jí)的學(xué)生以及班級(jí)信息
-- 按照要求顯示姓名、班級(jí)
-- 給數(shù)據(jù)表起名字
-- 查詢 有能夠?qū)?yīng)班級(jí)的學(xué)生以及班級(jí)信息,顯示學(xué)生的所有信息 students.*,只顯示班級(jí)名稱 classes.name.
-- 在以上的查詢中,將班級(jí)名顯示在第1列
-- 查詢 有能夠?qū)?yīng)班級(jí)的學(xué)生以及班級(jí)信息, 按照班級(jí)名進(jìn)行排序
-- 當(dāng)時(shí)同一個(gè)班級(jí)的時(shí)候,按照學(xué)生的id進(jìn)行從小到大排序
-- left join
-- 查詢每位學(xué)生對(duì)應(yīng)的班級(jí)信息
-- 查詢沒(méi)有對(duì)應(yīng)班級(jí)信息的學(xué)生
-- right join on
-- 將數(shù)據(jù)表名字互換位置,用left join完成
-- 子查詢
-- 標(biāo)量子查詢: 子查詢返回的結(jié)果是一個(gè)數(shù)據(jù)(一行一列)
-- 列子查詢: 返回的結(jié)果是一列(一列多行)
-- 行子查詢: 返回的結(jié)果是一行(一行多列)
-- 查詢出高于平均身高的信息(height)
-- 查詢學(xué)生的班級(jí)號(hào)能夠?qū)?yīng)的 學(xué)生名字
--數(shù)據(jù)操作前的準(zhǔn)備
--創(chuàng)建數(shù)據(jù)庫(kù)表
-- create table areas(
-- aid int primary key,
-- atitle varchar(20),
-- pid int
-- );
--從sql文件中導(dǎo)入數(shù)據(jù)
-- source 具體地址/areas.sql;
--查詢一共有多少個(gè)省
--例1:查詢省的名稱為“山西省”的所有城市
--例2:查詢市的名稱為“廣州市”的所有區(qū)縣
5. Mysql基本使用(2)
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
相關(guān)閱讀更多精彩內(nèi)容
- 目錄 一、查詢準(zhǔn)備 二、條件查詢 三、排序 四、聚合函數(shù) 五、分組 六、分頁(yè) 七、連接查詢 八、自關(guān)聯(lián) 九、子查詢...
- -- 基本查詢 -- 查詢所有字段 -- select * from 表名; select * from stud...
- pyspark.sql模塊 模塊上下文 Spark SQL和DataFrames的重要類: pyspark.sql...
- 數(shù)據(jù)庫(kù)分類 關(guān)系型數(shù)據(jù)庫(kù)庫(kù):Relational Database Management System (RDBM...
- 數(shù)據(jù)庫(kù)、表的創(chuàng)建 創(chuàng)建數(shù)據(jù)庫(kù)create database python_test_1 charset=utf8;...