MySQL基礎筆記 條件查詢

2021-09-12 23:50:07 字數 2713 閱讀 2838

語法:使用where關鍵字

select 查詢列表 from 表名 where 篩選條件

按條件表示式篩選:條件運算子 > < = != <> >= <=(sql語句的不等號一般寫為<>而不用!=)

按邏輯表示式篩選:邏輯運算子 and or not

(3)模糊查詢: like 、between and、in、is null

一、 按表示式篩選

篩選表中工資大於12000的員工資訊

select * from employees where salary>12000;

篩選部門編號不等於90的員工名和部門編號

select first_name, last_name, department_id from employees where department_id <> 90;

二、按邏輯表示式篩選

查詢工資在10000到20000之間的員工名、工資和獎金:

select last_name, salary, commission_pct from employees where salary >= 10000 and salary <= 20000;

查詢部門編號不是在90到110之間的,或者工資高於15000的員工資訊:

select * from employees where salary >= 15000 or department_id > 110 or department_id < 90;

或者使用not和and的組合來篩選部門編號:

select * from employees where not (department_id >= 90 and department_id <= 110) or salary >= 15000;

三、模糊查詢

(1)like,通常與萬用字元搭配使用

萬用字元:

查詢員工名中包含字元a的員工資訊

select * from employees where last_name like '%a%'

查詢員工名中第三個字元為n,第五個字元為l的員工名稱

select last_name from employees where last_name like '__n_l%'

查詢員工名第二個字元為_的員工名稱(需要用到轉義字元\)

select last_name from employees where last_name like '_\_%'

轉義字元可以自己定義,如這裡可以定義katex parse error: expected group after '_' at position 77: …ast_name like '_̲_%』 escape $;`

(2)使用between and進行範圍條件查詢

查詢員工編號在100到200之間的員工資訊:between and可對取同一欄位某一區間的數值時使用,**更為簡單。

注意:between a and b,a要<=b,且查詢出的結果是包含兩個臨界值的。(如下式查詢結果是包含100和120的)

select * from employees where employee_id between 100 and 120

(3)使用in判斷某一值是否屬於in列表中的某一項

注意,in列表中的內容型別應一致,且in列表中不支援使用萬用字元。

查詢員工工種編號是it_prog、ad_vp、ad_pres其中之一的員工名和工種編號:

select last_name,job_id from employees where job_id in ('it_prog','ad_vp','ad_pres')

(4)is null:null值不可直接用=查詢,查詢null值需要使用is 或者 is not

查詢沒有獎金的員工名和獎金率

select last_name,commission_pct from employees where commission_pct is null

(5)安全等於 <=>

is只能和null匹配使用,=適合查詢非null值,而安全等於<=>兩者皆可查,但是**可讀性較差。

示例:select last_name,commission_pct from employees where commission_pct <=> null

或者select last_name,salary from employees where salary <=> 12000

提取碼:yysu

mysql基礎查詢和條件查詢

資料庫的好處 1.持久化資料到本地 2.可以實現結構化查詢,方便管理 資料庫的相關概念 sql優點 資料庫儲存資料的特點 mysql服務的啟動和停止 mysql服務端的登入和退出 退出 exit 檢視mysql資料庫的版本 mysql的常用命令 create table 表名 列名 列型別,列名 列...

mysql基礎複習 條件查詢

selece 查詢列表 from 表名 where 篩選條件 篩選條件分類1 條件運算子 大於 小於 大於等於 小於等於 2 邏輯表示式 與 或 and 或 或 or 非 或 not 3 模糊查詢 like 指定子句的查詢模式,一般配合萬用字元使用 between num1 and num2 操作符...

MySQL學習筆記 條件查詢

1 普通條件查詢 語法 select col list from table name where condition expression 示例 查詢qq號為12301的玩家資訊 select from users where user qq 12301 示例 查詢分數大於2500分的資料 sel...