Mysql查詢語句詳細(xì)總結(jié)大全
更新時間:2023年12月19日 10:42:47 作者:冬嬙姐姐
這篇文章主要給大家介紹了關(guān)于Mysql查詢語句詳細(xì)總結(jié)的相關(guān)資料,MySQL是一種關(guān)系型數(shù)據(jù)庫管理系統(tǒng),它支持SQL語言進(jìn)行數(shù)據(jù)查詢,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
簡單查詢
## 直接查詢 語法:select 字段 from 表名; 舉例:select name, age from student; 解析:從 student 表中查詢 name 與 age
## 條件查詢 語法:select 字段 from 表名 where 條件; 舉例:select name from student where age = 15; 解析:從 student 表中查詢 age = 15 的 name
## 模糊查詢 語法:select 字段 from 表名 where 字段 like '%數(shù)據(jù)%'; 舉例:select * from student where name like '%張%'; 解析:從 student 表中查詢 name 中含有 '張' 的所有記錄
## 算術(shù)運(yùn)算符 語法:>(大于), <(小于), =(等于), !=(不等于), <>(不等于), >=(大于等于), <=(小于等于) 舉例:select * from student where age < 15; 解析:從 student 表中查詢 age < 15 的所有記錄
## 邏輯運(yùn)算符 語法:and(且), or(或), not(非) 舉例:select * from student where age = 15 or sex = 'man'; 解析:從 student 表中查詢 age = 15 或 sex = 'man' 的所有記錄
## in與not in運(yùn)算符 語法:select 字段 from 表名 where 字段 in(列表)//或 not in(列表); 舉例:select * from student where age in(13, 14, 15); 解析:從 student 表中查詢 age 為 (13, 14, 15) 之間的所有記錄
## 排序查詢 語法:select 字段 from 表名 order by 字段 排序方式(升序 asc, 降序 desc); 舉例:select * from student order by age asc 解析:從 student 表中查詢所有記錄并按照 age 升序排序
高級查詢
## 范圍運(yùn)算 語法:用來替換算術(shù)運(yùn)算符 select 字段 from 表名 where 字段 between 范圍1 and 范圍2; 舉例:select * from student where age between 13 and 15; 解析:從 student 表中查詢 age >= 13 and age <= 15 的所有記錄 它等價于 select * from student where age >= 13 and age <= 15;
## 限制查詢 語法:limit可以限制制定查詢結(jié)果的記錄條數(shù) select 字段 from 表名 limit n, m; 舉例:select * from student limit 3, 5; 解析:從 student 表中查詢第三行到第五行的記錄,但要注意的是 0 表示第一行記錄,也是從 0 開始
## 嵌套查詢 語法:嵌套查詢也就是在查詢語句中包含有子查詢語句,所以叫嵌套查詢,沒有單獨的語法,嵌套子查詢通常位于查詢語句的條件之后; 舉例:select name, age from student where name = (select name from engScore where score = 100) 解析:查詢 student 表中 (engScore 表中 score = 100 的 name)的 name,age 記錄 也就是說通過查詢 engScore 表中的一百分得到姓名,然后用這個姓名當(dāng)作條件查詢 student 表中的姓名與年齡
## 多表連查 語法:與嵌套查詢一樣,都需要一個共同字段,然后將多個表連接在一起查詢,將符合條件的記錄組成一個合集 常用以下三種連接方式: # 內(nèi)連接 語法:select 字段 from 表1 inner join 表2 on 表1.字段 = 表2.字段; 根據(jù)兩個表中共有的字段進(jìn)行匹配,然后將符合條件的合集進(jìn)行拼接 on后面是連接條件,也就是共有字段 舉例:select * from student inner join engScore on student.name = engScore.name; 解析:將 student 表與 engScore 表通過相同的 name 拼接起來,簡單的來說就是兩個 excel 合并 # 左連接 語法:select 字段 from 表1 left join 表2 on 連接條件; 舉例:select * from student left join engScore on student.name = engScore.name; 解析:與內(nèi)連接形式相同,但左表為主表,指定字段都會顯示,右表為從表,無內(nèi)容會顯示 null # 右連接 語法:select 字段 from 表1 right join 表2 on 連接條件; 舉例:select * from student right join engScore on student.name = engScore.name; 解析:與內(nèi)連接形式相同,但右表為主表,指定字段都會顯示,左表為從表,無內(nèi)容會顯示 null
## 聚合函數(shù) 可以實現(xiàn)一些具體的功能,比如找最小值,找最大值,求和,計數(shù)等 # min() 語法:select min(字段) from 表名; 舉例:select min(age) from student; 解析:從 student 中查詢最小的 age # max() 語法:select max(字段) from 表名; 舉例:select max(age) from student; 解析:從 student 中查詢最大的 age # sum() 語法:select sum(字段) from 表名; 舉例:select sum(age) from student; 解析:從 student 中統(tǒng)計所有 age 的和 # avg() 語法:select avg(字段) from 表名; 舉例:select avg(age) from student; 解析:從 student 中對所有的 age 求平均值 # count() 語法:select count(字段) from 表名; 舉例:select count(name) from student; 解析:從 student 中查詢 name 的記錄個數(shù) # as 語法: select 函數(shù)(字段) as 別名 from 表名; 舉例:select count(name) as 名字記錄個數(shù) from student; 解析:給從 student 中查詢的 name 的記錄個數(shù) 起了一個別名叫 '名字記錄個數(shù)'
## 大小寫轉(zhuǎn)換 語法:select upper(字段) from 表名; 舉例:select upper(sex) from student where name = '張三'; 解析:若原 sex 定義為 man, 則運(yùn)行 sql 語句之后會輸出 MAN
總結(jié)
到此這篇關(guān)于Mysql查詢語句詳細(xì)總結(jié)大全的文章就介紹到這了,更多相關(guān)Mysql查詢語句內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Ubuntu 20.04 安裝和配置MySql5.7的詳細(xì)教程
這篇文章主要介紹了Ubuntu 20.04 安裝和配置MySql5.7的相關(guān)資料,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12mysql之跨庫關(guān)聯(lián)查詢(dblink)問題
這篇文章主要介紹了mysql之跨庫關(guān)聯(lián)查詢(dblink)問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03mysql實現(xiàn)將字符串字段轉(zhuǎn)為數(shù)字排序或比大小
這篇文章主要介紹了mysql實現(xiàn)將字符串字段轉(zhuǎn)為數(shù)字排序或比大小,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06