亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

MySQL?條件查詢詳解

 更新時間:2023年05月29日 11:41:31   作者:wxl@  
這篇文章主要介紹了MySQL條件查詢,本文通過示例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

條件查詢

語法:

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

分類:

  • 按條件表達式篩選:條件運算符:> < = != >= <=
  • 按邏輯表達式篩選:邏輯運算符:&& || ! 或者:and or not
  • 模糊查詢:like, between and, in, is null.

首先看看按條件表達式查詢:

# 查詢工資大于12000的員工:
select * from employees where salary > 12000;
# 部門編號不等于90的員工名和部門編號
select department_ID, sname from employees where department_ID != 90;

按邏輯表達式篩選:

# 查看工資在10000到20000之間的員工名和工資
select sname, salary from employees where 
salary >=10000 and salary <= 20000;
# 查看部門編號不是在90到110之間并且工資高于20000的員工信息
select * from employees where department_ID<90 or department_ID>110 
or salary > 20000;

模糊查詢

like:

 # 查詢員工名中包含字符'a'的員工信息
 # 這里注意,MySQL中不區(qū)分大小寫,所以名字中如果有大寫的A也是包含進去的
 select * from temp where sname like '%a%'; /* 字符型一定只能是單引號,
 因為字符a的前后可能還有其他的字符,所以這里要在a的前后加上%,表示通配符*/

總結(jié)一下like的特點:

一般和通配符配合使用
常見通配符?
% :任意多個字符,包含0個
_ :下劃線,任意的單個字符

# 查詢員工姓名中第三個字符是'a'第五個是'e'的員工信息:
select * from employees where name like '__a_e%';
# 查詢員工姓名中第二個字符是_的員工信息:
select * from employees where name like '_\_%';# 在這種like關(guān)鍵字模糊查詢中可以使用轉(zhuǎn)義字符\...
# 如果不想使用\也可以使用escape語句
select * from employees where name like '_$_%' escape '$';

between and:(簡化and)

select * from employees where
salary between 120 and 200;

特點:

  • 可以簡化代碼。
  • 包含兩個臨界值(閉區(qū)間)。
  • 不可以將兩個臨界值互換。

in(簡化or):

# 查詢員工工種編號是AD_UI, AD_YU中的一個的員工的姓名和工號
select sname, job_id from employees where 
job_id='AD_UI' or job_id='AD_YU';
# 使用in:
select sname, job_id from employees where
job_id in ('AD_UI', 'AD_YU');
# 特殊情況:如果將最后一句改成job_id in ('AD%');是否可行?
select sname, job_id from employees where
job_id in ('AD%');# 這是不可行的,因為通配符只能在like語句中使用

is null:

注意事項:
不可以使用“=”來判斷值是否為空:
is 和null應(yīng)該是連在一起用的,不能判斷其他的如:is 200

select * from employees where
salary = null;  # 這是錯誤的表示方式
select * from employees where
salary is null;  # 這是正確的表示方式
select * from employees where 
salary is 2000;  # 會報錯
select * from employees where
salary is not null;  # 判斷不是空

安全等于 <=>

安全等于可以判斷null也可以用來判斷常量值
可讀性不高,一眼看去無法判斷是等于還是不等于

select * from employees where
salary <=> null;  # 判斷為空
select * from employees where 
salary <=> 12000;  # 判斷常數(shù)值

到此這篇關(guān)于MySQL 條件查詢 的文章就介紹到這了,更多相關(guān)mysql條件查詢 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論