mysql查询条件是小数 查不到6.28_28.mysql数据库之查询
1.查詢語句
mysql 多表關(guān)系 查詢語句 索引
1.添加數(shù)據(jù)補(bǔ)充:
將一個查詢結(jié)果插入到另一張表中
create table student(name char(10),gender int);
insert into student values("jack",1);
insert into student values("rose",0);
create table student_man(name char(10),gender int);
insert into student_man select * from student where gender = 1;
2.所有的select 關(guān)鍵字
select distinct * from table_name
where
group by
having
order by
limit a,b
必須存在的有:
select
* 可以換成任意的一個或多個字段名稱
from
table_name
#注意: 關(guān)鍵字的順序是固定的不能隨意變化
3.where 條件
select * from table_name
where
where 后面可以是
1.比較運(yùn)算符
> < >= <= = !=
2.成員運(yùn)算符
in not in 后面是一個set
3.邏輯運(yùn)算符
and or not
not 要放在表達(dá)式的前面 and 和 or 放到兩個表達(dá)式中間
4.模糊查詢
like
% 表示 任意個數(shù)的任意字符
_ 表示一個任意字符
#
請查詢 姓小的 數(shù)學(xué)小于 80 分 并且 英語 > 20分 的人的 數(shù)學(xué)成績
select math,name from stu where math < 80 and english > 20 and name like "小%";
4.distinct 去除重復(fù)記錄
select distinct * from stu;
# 注意僅當(dāng)查詢結(jié)果中所有字段全都相同時 才算重復(fù)的記錄
5.指定字段
1.星號表示所有字段
2.手動指定需要查詢的字段
3.還可也是四則運(yùn)算
4.聚合函數(shù)
#請查詢 英語及格的人的 平均分
select name,(math+english) / 2 平均分 from stu where english >= 60;
6.取別名
select name,math+english as 總分 from stu where name = "趙云";
as 可以省略
統(tǒng)計函數(shù)
也稱之為聚合函數(shù)
將一堆數(shù)據(jù)經(jīng)過計算得出一個結(jié)果
求和 sum(字段名)
平均數(shù) avg(字段名)
最大值 max(字段名)
最小值 min(字段名)
個數(shù) count(字段名) # 字段名稱可以使用* 代替 另外如果字段為空會被忽略
可以用在 字段的位置 或是分組的后面
例如: 查詢所有人的平均工資
select avg(salary) from emp
錯誤案例: 查詢工資最高的人的姓名
select name,max(salary) from emp;
#默認(rèn)顯示的第一個name 因為name有很多行 而max(salary) 只有一行 兩列的行數(shù)不匹配
# 不應(yīng)該這么寫 邏輯錯誤
select name from emp where salary = max(salary);
# 報錯
# 原因: 偽代碼
for line in file:
if salary = max(salary) #
#分析 where 讀取滿足條件的一行 ,max()先要拿到所有數(shù)據(jù) 才能求最大值,
#這里由于讀取沒有完成所有無法 求出最大值
#結(jié)論 where 后面不能使用聚合函數(shù)
7.group by
group 是分組的意思 即將一個整體按照某個特征或依據(jù)來分為不同的部分
為什么要分組 分組是為了統(tǒng)計,例如統(tǒng)計男性有幾個 女性有幾個
語法:
select xxx from table_name group by 字段名稱;
需求:統(tǒng)計每個性別有幾個人
select sex,count(*) from emp group by sex;
需求: 查詢每個性別有幾個 并且顯示名字
select name,sex,count(*) from emp group by sex;
# mysql 5.6下 查詢的結(jié)果是name僅顯示該分組下的第一個
# 5.7以上則直接報錯 ,5.6也可以手動開啟這個功能
# 我們可以用group_concat 將分組之外的字段 做一個拼接 ,但是這是沒有意義
# 如果要查詢某個性別下的所有信息 直接使用where 即可
#結(jié)論: 只有出現(xiàn)在了group by 后面得字段才能出現(xiàn)在select的后面
8.having
用于過濾,但是與where不同的是,having使用在分組之后
案例:
# 求出平均工資大于500的部門信息
select dept,avg(salary) from emp group by dept having avg(salary) > 5000;
#查詢 部門人數(shù)少于3的 部門名稱 人員名稱 人員個數(shù)
select dept,group_concat(name),count(*) from emp group by dept having count(*) < 3;
9.order
根據(jù)某個字段排序
語法:
select * from table_name order by 字段名稱;
# 默認(rèn)是升序
# 改為降序
select * from table_name order by 字段名稱 desc;
# 多個字段 第一個相同在按照第二個 asc 表示升序
select * from table_name order by 字段名稱1 desc,字段名稱2 asc;
案例:
select * from emp order by salary desc,id desc;
10.limit
用于限制要顯示的記錄數(shù)量
語法1:
select * from table_name limit 個數(shù);
語法2:
select * from table_name limit 起始位置,個數(shù);
# 查詢前三條
select * from emp limit 3;
# 從第三條開始 查詢3條 3-5
select * from emp limit 2,3;
# 注意:起始位置 從0開始
# 經(jīng)典的使用場景:分頁顯示
1.每一頁顯示的條數(shù) a = 3
2.明確當(dāng)前頁數(shù) b = 2
3.計算起始位置 c = (b-1) * a
select * from emp limit 0,3;
select * from emp limit 3,3;
select * from emp limit 6,3;
# django 提供了現(xiàn)成的分頁組件 但是它是先查詢所有數(shù)據(jù) 丟到列表中 再取出數(shù)據(jù) 這樣如果數(shù)據(jù)量太大可能會有問題
2.子查詢
將一個查詢語句的結(jié)果作為另一個查詢語句的條件或是數(shù)據(jù)來源
當(dāng)我們一次性查不到想要數(shù)據(jù)時就需要使用子查詢
1.in 關(guān)鍵字子查詢
當(dāng)內(nèi)層查詢 (括號內(nèi)的) 結(jié)果會有多個結(jié)果時, 不能使用 = 必須是in ,另外子查詢必須只能包含一列數(shù)據(jù)
需求: 指定一個部門名稱,獲取改部門下的所有員工信息
1.查詢出 平均年齡 大于25的部門編號
select dept_id from emp group by dept_id having avg(age) > 25;
2.再根據(jù)編號查詢部門的名稱
select name from dept where id in (select dept_id from emp group by dept_id having avg(age) > 25);
子查詢的思路:
1.要分析 查到最終的數(shù)據(jù) 到底有哪些步驟
2.根據(jù)步驟寫出對應(yīng)的sql語句
3.把上一個步驟的sql語句丟到下一個sql語句中作為條件
2.exists 關(guān)鍵字子查詢
當(dāng)內(nèi)層查詢 有結(jié)果時 外層才會執(zhí)行
案例:
select* from dept where exists (select * from dept where id = 1);
# 由于內(nèi)層查詢產(chǎn)生了結(jié)果 所以 執(zhí)行了外層查詢dept的所有數(shù)據(jù)
3.多表查詢
1.笛卡爾積查詢
select * from table1,table2,......
# 笛卡爾積查詢的結(jié)果會出現(xiàn)大量的錯誤數(shù)據(jù)即,數(shù)據(jù)關(guān)聯(lián)關(guān)系錯誤!
添加過濾條件 從表外鍵值 等于 主表的主鍵值
# 并且會產(chǎn)生重復(fù)的字段信息 例如員工里的 部門編號 和 部門表的id字段
在select 后指定需要查詢的字段名稱
案例:
select dept.name 部門 ,dept.id 部門編號,emp.name 姓名,emp.id 員工編號,sex from emp ,dept where dept.id = dept_id;
2.內(nèi)連接查詢:
本質(zhì)上就是笛卡爾積查詢
語法:
select * from table1 inner join table2;
案例:
select * from emp inner join dept where dept_id = dept.id;
inner可以省略
select * from emp join dept where dept_id = dept.id;
3.左外連接查詢
左邊的表無論是否能夠匹配都要完整顯示
右邊的僅展示匹配上的記錄
需求: 要查詢所有員工以及其所屬的部門信息
select * from emp left join dept on dept_id = dept.id;
注意: 在外連接查詢中不能使用where 關(guān)鍵字 必須使用on專門來做表的對應(yīng)關(guān)系
4.右外連接查詢
右邊的表無論是否能夠匹配都要完整顯示
左邊的僅展示匹配上的記錄
需求: 要查詢所有部門以及其對應(yīng)的員工信息
select * from emp right join dept on dept_id = dept.id;
5.全外連接查詢
無論是否匹配成功 兩邊表的數(shù)據(jù)都要全部顯示
需求:查詢所有員工與所有部門的對應(yīng)關(guān)系
select * from emp full join dept on dept_id = dept.id;
注意:mysql不支持全外連接
我們可以將 左外連接查詢的結(jié)果 和 右外連接查詢的結(jié)果 做一個合并
select * from emp left join dept on dept_id = dept.id
union
select * from emp right join dept on dept_id = dept.id;
union的用法:
select * from emp
union
select * from emp;
# union將自動去除重復(fù)的記錄
# union all 不去重復(fù)
select sex,name from emp
union
select * from dept;
# 注意 union 必須保證兩個查詢結(jié)果 列數(shù)相同 一般用在多個結(jié)果結(jié)構(gòu)完全一致時
總結(jié): 外連接查詢 查到的是沒有對應(yīng)關(guān)系的記錄,但是這樣的數(shù)據(jù)原本就是有問題的,所以最常用的是內(nèi)連接查詢
內(nèi)連接表示 只顯示匹配成功的記錄
外連接 沒有匹配成功的也要實現(xiàn)
多表查詢案例:
create table stu(id int primary key auto_increment,name char(10));
create table tea(id int primary key auto_increment,name char(10));
create table tsr(id int primary key auto_increment,t_id int,s_id int,
foreign key(s_id) references stu(id),
foreign key(t_id) references tea(id));
insert into stu values(null,"張三"),(null,"李四");
insert into tea values(null,"egon"),(null,"wer");
insert into tsr values(null,1,1),(null,1,2),(null,2,2);
#egon老師教過哪些人?
select tea.name,stu.name from tea join tsr join stu
on
tea.id = t_id and stu.id = s_id
where tea.name = "egon";
# 子查詢實現(xiàn)
select * from stu where id in (select s_id from tsr where t_id = (select id from tea where name = "egon"));
小結(jié):
select [distinct] *|字段名|四則運(yùn)算|函數(shù) from table_name
where 比較運(yùn)算符 邏輯運(yùn)算符 成員運(yùn)算符 區(qū)間 between and 模糊匹配 like exists regexp 正則匹配
group by
having 通常根聚合函數(shù) count sum max min avg
order by
limit a,b
總結(jié)
以上是生活随笔為你收集整理的mysql查询条件是小数 查不到6.28_28.mysql数据库之查询的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 抽象类可以用new创建对象吗_宠物可以用
- 下一篇: mysql 中varchar_MYSQL