日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

mysql查询条件是小数 查不到6.28_28.mysql数据库之查询

發(fā)布時間:2025/3/15 数据库 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mysql查询条件是小数 查不到6.28_28.mysql数据库之查询 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1.查詢語句

mysql 多表關系 查詢語句 索引

1.添加數據補充:

將一個查詢結果插入到另一張表中

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 關鍵字

select distinct * from table_name

where

group by

having

order by

limit a,b

必須存在的有:

select

* 可以換成任意的一個或多個字段名稱

from

table_name

#注意: 關鍵字的順序是固定的不能隨意變化

3.where 條件

select * from table_name

where

where 后面可以是

1.比較運算符

> < >= <= = !=

2.成員運算符

in not in 后面是一個set

3.邏輯運算符

and or not

not 要放在表達式的前面 and 和 or 放到兩個表達式中間

4.模糊查詢

like

% 表示 任意個數的任意字符

_ 表示一個任意字符

#

請查詢 姓小的 數學小于 80 分 并且 英語 > 20分 的人的 數學成績

select math,name from stu where math < 80 and english > 20 and name like "小%";

4.distinct 去除重復記錄

select distinct * from stu;

# 注意僅當查詢結果中所有字段全都相同時 才算重復的記錄

5.指定字段

1.星號表示所有字段

2.手動指定需要查詢的字段

3.還可也是四則運算

4.聚合函數

#請查詢 英語及格的人的 平均分

select name,(math+english) / 2 平均分 from stu where english >= 60;

6.取別名

select name,math+english as 總分 from stu where name = "趙云";

as 可以省略

統(tǒng)計函數

也稱之為聚合函數

將一堆數據經過計算得出一個結果

求和 sum(字段名)

平均數 avg(字段名)

最大值 max(字段名)

最小值 min(字段名)

個數 count(字段名) # 字段名稱可以使用* 代替 另外如果字段為空會被忽略

可以用在 字段的位置 或是分組的后面

例如: 查詢所有人的平均工資

select avg(salary) from emp

錯誤案例: 查詢工資最高的人的姓名

select name,max(salary) from emp;

#默認顯示的第一個name 因為name有很多行 而max(salary) 只有一行 兩列的行數不匹配

# 不應該這么寫 邏輯錯誤

select name from emp where salary = max(salary);

# 報錯

# 原因: 偽代碼

for line in file:

if salary = max(salary) #

#分析 where 讀取滿足條件的一行 ,max()先要拿到所有數據 才能求最大值,

#這里由于讀取沒有完成所有無法 求出最大值

#結論 where 后面不能使用聚合函數

7.group by

group 是分組的意思 即將一個整體按照某個特征或依據來分為不同的部分

為什么要分組 分組是為了統(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下 查詢的結果是name僅顯示該分組下的第一個

# 5.7以上則直接報錯 ,5.6也可以手動開啟這個功能

# 我們可以用group_concat 將分組之外的字段 做一個拼接 ,但是這是沒有意義

# 如果要查詢某個性別下的所有信息 直接使用where 即可

#結論: 只有出現(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;

#查詢 部門人數少于3的 部門名稱 人員名稱 人員個數

select dept,group_concat(name),count(*) from emp group by dept having count(*) < 3;

9.order

根據某個字段排序

語法:

select * from table_name order by 字段名稱;

# 默認是升序

# 改為降序

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

用于限制要顯示的記錄數量

語法1:

select * from table_name limit 個數;

語法2:

select * from table_name limit 起始位置,個數;

# 查詢前三條

select * from emp limit 3;

# 從第三條開始 查詢3條 3-5

select * from emp limit 2,3;

# 注意:起始位置 從0開始

# 經典的使用場景:分頁顯示

1.每一頁顯示的條數 a = 3

2.明確當前頁數 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)成的分頁組件 但是它是先查詢所有數據 丟到列表中 再取出數據 這樣如果數據量太大可能會有問題

2.子查詢

將一個查詢語句的結果作為另一個查詢語句的條件或是數據來源

當我們一次性查不到想要數據時就需要使用子查詢

1.in 關鍵字子查詢

當內層查詢 (括號內的) 結果會有多個結果時, 不能使用 = 必須是in ,另外子查詢必須只能包含一列數據

需求: 指定一個部門名稱,獲取改部門下的所有員工信息

1.查詢出 平均年齡 大于25的部門編號

select dept_id from emp group by dept_id having avg(age) > 25;

2.再根據編號查詢部門的名稱

select name from dept where id in (select dept_id from emp group by dept_id having avg(age) > 25);

子查詢的思路:

1.要分析 查到最終的數據 到底有哪些步驟

2.根據步驟寫出對應的sql語句

3.把上一個步驟的sql語句丟到下一個sql語句中作為條件

2.exists 關鍵字子查詢

當內層查詢 有結果時 外層才會執(zhí)行

案例:

select* from dept where exists (select * from dept where id = 1);

# 由于內層查詢產生了結果 所以 執(zhí)行了外層查詢dept的所有數據

3.多表查詢

1.笛卡爾積查詢

select * from table1,table2,......

# 笛卡爾積查詢的結果會出現(xiàn)大量的錯誤數據即,數據關聯(lián)關系錯誤!

添加過濾條件 從表外鍵值 等于 主表的主鍵值

# 并且會產生重復的字段信息 例如員工里的 部門編號 和 部門表的id字段

在select 后指定需要查詢的字段名稱

案例:

select dept.name 部門 ,dept.id 部門編號,emp.name 姓名,emp.id 員工編號,sex from emp ,dept where dept.id = dept_id;

2.內連接查詢:

本質上就是笛卡爾積查詢

語法:

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 關鍵字 必須使用on專門來做表的對應關系

4.右外連接查詢

右邊的表無論是否能夠匹配都要完整顯示

左邊的僅展示匹配上的記錄

需求: 要查詢所有部門以及其對應的員工信息

select * from emp right join dept on dept_id = dept.id;

5.全外連接查詢

無論是否匹配成功 兩邊表的數據都要全部顯示

需求:查詢所有員工與所有部門的對應關系

select * from emp full join dept on dept_id = dept.id;

注意:mysql不支持全外連接

我們可以將 左外連接查詢的結果 和 右外連接查詢的結果 做一個合并

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將自動去除重復的記錄

# union all 不去重復

select sex,name from emp

union

select * from dept;

# 注意 union 必須保證兩個查詢結果 列數相同 一般用在多個結果結構完全一致時

總結: 外連接查詢 查到的是沒有對應關系的記錄,但是這樣的數據原本就是有問題的,所以最常用的是內連接查詢

內連接表示 只顯示匹配成功的記錄

外連接 沒有匹配成功的也要實現(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"));

小結:

select [distinct] *|字段名|四則運算|函數 from table_name

where 比較運算符 邏輯運算符 成員運算符 區(qū)間 between and 模糊匹配 like exists regexp 正則匹配

group by

having 通常根聚合函數 count sum max min avg

order by

limit a,b

總結

以上是生活随笔為你收集整理的mysql查询条件是小数 查不到6.28_28.mysql数据库之查询的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。