一.簡單查詢--select
1.用處:查詢單一字段、查詢連續的多個字段、查詢所有字段
2.用法:
查詢字段 | 語句 | 備注 |
單一字段 | select name from userInfo; |
關鍵字:select、from;標識符:字段名、表名
|
連續多個字段 | select name,age,email from userInfo; |
/ |
所有字段 | select * from userInfo; |
缺點:效率低、可讀性差 |
二.條件查詢--where
1.什么是條件查詢?
僅查詢復合條件的數據。
語法格式:select 字段1,字段2,字段3 from 表名 where 條件;
2.條件查詢的分類:
共計13種,分別為:>, <, >=, <=, =, <>或!=, between...and... , is null, and, or, in( , , , ), not in( , , , ), not
含義 | 分類 | 備注 |
等於 | = | / |
不等於 | <>或!= | / |
兩值之間 | between...and... | 等價於:>= and <= |
記錄查詢為null的記錄 | is null | 數據庫中null不能使用 = 進行衡量,需要使用is null ∵ 數據庫中的null代表什么也沒有,不是一個值 |
取非 | not | 主要用在 is null 和 in 中 |
並且 | and | and優先級比or高,若需or先執行,需要加“()” |
或者 | or | |
包含 | in( , , , ) |
in相當於多個or。in不是一個區間。in后面跟的是具體值。 |
不在()內數值中的幾個值的數據 | not in( , , , ) | select ename,sal from emp where sal not in(800, 5000, 3000); |
分類 | 語句 | 備注 |
= | select sal from emp where ename = 'SMITH'; | 字符串使用單引號 |
<> / != | select ename from emp where sal != 800; | ... sal <> 800; |
between..and.. | select ename,sal from emp where sal between 2450 and 3000; | 左小右大,閉區間 |
is null | select ename,sal,comm from emp where comm is null; | / |
and | select ename,job,sal from emp where job = 'tech' and sal > 2500; |
/ |
or | select ename,job from emp where job = 'tech' or job = 'salsman'; | / |
in( , , , ) | select ename,job from emp where job in ('tech', 'salsman'); | 括號內是具體值 |
三.模糊查詢--like
1.支持 % 或 _ 匹配。
%:匹配任意 多個 字符 開頭、結尾標識符
_:匹配任意 一個 字符 占位符
2.示例: