千鋒教育2023新版javaweb速成全套教程,零基礎(chǔ)入門到企業(yè)項(xiàng)目實(shí)戰(zhàn)

MySQL數(shù)據(jù)庫是后端程序員必須要掌握的點(diǎn),其中MySQL查詢語句,不管是程序員還是初學(xué)者都是必須掌握的,下面是我在學(xué)習(xí)視頻后對常見查詢語句的總結(jié):
## 直接查詢
語法: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 升序排序
以上是常見的簡單查詢,接下來查詢會出現(xiàn)聚合函數(shù)、多表查詢;
## 范圍運(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 開始
## 嵌套查詢
語法:嵌套查詢也就是在查詢語句中包含有子查詢語句,所以叫嵌套查詢,沒有單獨(dú)的語法,嵌套子查詢通常位于查詢語句的條件之后;
舉例: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ù)
可以實(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