Mysql查詢語句詳細總結(jié)大全
更新時間:2023年12月19日 10:42:47 作者:冬嬙姐姐
這篇文章主要給大家介紹了關(guān)于Mysql查詢語句詳細總結(jié)的相關(guān)資料,MySQL是一種關(guān)系型數(shù)據(jù)庫管理系統(tǒng),它支持SQL語言進行數(shù)據(jù)查詢,文中通過代碼介紹的非常詳細,需要的朋友可以參考下
簡單查詢
## 直接查詢 語法: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ù)運算符 語法:>(大于), <(小于), =(等于), !=(不等于), <>(不等于), >=(大于等于), <=(小于等于) 舉例:select * from student where age < 15; 解析:從 student 表中查詢 age < 15 的所有記錄
## 邏輯運算符 語法:and(且), or(或), not(非) 舉例:select * from student where age = 15 or sex = 'man'; 解析:從 student 表中查詢 age = 15 或 sex = 'man' 的所有記錄
## in與not in運算符 語法: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 升序排序
高級查詢
## 范圍運算 語法:用來替換算術(shù)運算符 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 表中的一百分得到姓名,然后用這個姓名當作條件查詢 student 表中的姓名與年齡
## 多表連查 語法:與嵌套查詢一樣,都需要一個共同字段,然后將多個表連接在一起查詢,將符合條件的記錄組成一個合集 常用以下三種連接方式: # 內(nèi)連接 語法:select 字段 from 表1 inner join 表2 on 表1.字段 = 表2.字段; 根據(jù)兩個表中共有的字段進行匹配,然后將符合條件的合集進行拼接 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, 則運行 sql 語句之后會輸出 MAN
總結(jié)
到此這篇關(guān)于Mysql查詢語句詳細總結(jié)大全的文章就介紹到這了,更多相關(guān)Mysql查詢語句內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MySQL數(shù)據(jù)同步Elasticsearch的4種方案
本文主要介紹了MySQL數(shù)據(jù)同步Elasticsearch的4種方案,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03
MySQL數(shù)據(jù)庫修改密碼以及設(shè)置密碼永過期問題
這篇文章主要介紹了MySQL數(shù)據(jù)庫修改密碼以及設(shè)置密碼永過期問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-04-04
MySQL使用Replace操作時造成數(shù)據(jù)丟失的問題解決
這篇文章主要給大家介紹了關(guān)于MySQL使用Replace操作時造成數(shù)據(jù)丟失問題的解決方法,文中通過示例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用MySQL具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
SQL實現(xiàn)LeetCode(180.連續(xù)的數(shù)字)
這篇文章主要介紹了SQL實現(xiàn)LeetCode(180.連續(xù)的數(shù)字),本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-08-08
Mysql應(yīng)用安裝后找不到my.ini文件的解決過程
剛剛在修改mysql默認配置的時候,發(fā)現(xiàn)找不到my.ini文件,下面這篇文章主要給大家介紹了關(guān)于Mysql應(yīng)用安裝后找不到my.ini文件的解決過程,文中通過圖文介紹的非常詳細,需要的朋友可以參考下2022-08-08
mysql數(shù)據(jù)庫navicat數(shù)據(jù)同步時誤刪除部分數(shù)據(jù)的解決
本文主要介紹了mysql數(shù)據(jù)庫navicat數(shù)據(jù)同步時誤刪除部分數(shù)據(jù),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-04-04

