作用:檢索數據中符合條件的值
注意:搜索的條件由一個或者多個表達式組成!結果 布爾值
1.1、邏輯運算符
| 運算符 | 語法 | 描述 |
|---|---|---|
| and && | a and b a&&b | 邏輯與,兩個都為真,結果為真 |
| or || | a or b a|| b | 邏輯或,其中一個為真,則結果為真 |
| Not ! | not a !a | 邏輯非, 真為假,假為真! |
注意:盡量使用英文
-- =================== where ======================
SELECT studentNo,`StudentResult` FROM result
-- 查詢考試成績在 95~100 分之間
SELECT studentNo,`StudentResult` FROM result
WHERE StudentResult>=95 AND StudentResult<=100
-- and &&
SELECT studentNo,`StudentResult` FROM result
WHERE StudentResult>=95 && StudentResult<=100
-- 除了1000號學生之外的同學的成績
SELECT studentNo,`StudentResult` FROM result
WHERE studentNo!=1000;
-- != not
SELECT studentNo,`StudentResult` FROM result
WHERE NOT studentNo = 1000
1.2、模糊查詢 : 比較運算符
| 運算符 | 語法 | 描述 |
|---|---|---|
| IS NULL | a is null | 如果操作符為 NUll, 結果為真 |
| IS NOT NULL | a is not null | 如果操作符不為 null,結果為真 |
| BETWEEN | a between b and c | 若a 在 b 和c 之間,則結果為真 |
| Like | a like b | SQL 匹配,如果a匹配b,則結果為真 |
| In | a in (a1,a2,a3….) | 假設a在a1,或者a2…. 其中的某一個值中,結果為真 |
1.2.1、Like關鍵字
1、查詢姓劉的同學(%)
-- like結合 %(代表0到任意個字符) _(一個字符)
SELECT `StudentNo`,`StudentName` FROM `student`
WHERE StudentName LIKE '劉%'
效果:這里姓劉的同學的名不管有多少字都匹配

2、查詢姓劉的同學,名字后面只有一個字的(_)
SELECT `StudentNo`,`StudentName` FROM `student`
WHERE StudentName LIKE '劉_'
效果:

3、查詢姓劉的同學,名字后面只有兩個字的(__)
SELECT `StudentNo`,`StudentName` FROM `student`
WHERE StudentName LIKE '劉__'
效果:

4、查詢名字中間有嘉字的同學 (%嘉%)
SELECT `StudentNo`,`StudentName` FROM `student`
WHERE StudentName LIKE '%嘉%'
效果:

1.2.2、IN關鍵字
具體的一個或者多個值
1、查詢1001,1002,1003號學員
SELECT `StudentNo`,`StudentName` FROM `student`
WHERE StudentNo IN (1001,1002,1003);
效果:

2、查詢在河南洛陽的學生
SELECT `StudentNo`,`StudentName`,`address`FROM `student`
WHERE `address` IN ('河南洛陽')
效果:

1.2.3、IS NULL關鍵字
1、查詢地址為空的學生
SELECT `StudentNo`,`StudentName` FROM `student`
WHERE address='' OR address IS NULL
-- 查詢出來是為空
2、查詢沒有有出生日期的同學
SELECT `StudentNo`,`StudentName` FROM `student`
WHERE `BornDate` IS NULL
-- 查詢出來也是空
1.2.4、IS NOT NULL關鍵字
1、查詢有出生日期的同學
SELECT `StudentNo`,`StudentName`,`BornDate`FROM `student`
WHERE `BornDate` IS NOT NULL
效果:

1.2.4、BETWEEN關鍵字
1、查詢考試成績在 95~100 分之間的學生
SELECT studentNo,`StudentResult` FROM result
WHERE StudentResult BETWEEN 95 AND 100
效果:

