MySQL條件查詢和範圍查詢

2021-10-01 12:01:42 字數 1943 閱讀 6092

一、 比較運算子

運算子功能

示例》大於select * from student where age > 14;

<

小於select * from student where age < 18;

>=

大於等於

select * from student where age >= 15;

<=

小於等於

select * from student where age <= 16;

!=/<>

不等於select * from student where age != 14;

二、 邏輯運算子

運算子功能

示例and

與,多個條件同時成立

select * from student where age > 14 and age < 18;

or或,多個條件任意乙個成立

select * from student where age > 14 and gender = 『男』;

not非,取反

select * from student where not age > 14;

三、 模糊查詢

模糊查詢有兩種方法,like或者rlike。

——like:用%代替乙個或多個字元;用_代替乙個字元。

示例1:查詢名字中以「李」開頭的學生:

select * from student  where name like "李%";
示例2:查詢名字中含有「林」的學生:

select * from student where name like "%林%";
示例3:查詢有三個字的名字的學生:

select * from student where name like "___";
示例4:查詢有三個字以上的名字的學生:

select * from student where name like "___%";
——rlike:以正規表示式對字元進行匹配。

示例1:查詢名字中以「李」開頭,以「寧」結尾的學生:

select * from student  where name like "^李.*寧$";
四、 範圍查詢

——in:非連續的範圍查詢。

示例1:選擇年齡是14、16、18歲的學生:

select * from student where name in (14,16,18);
——between …… and ……:表示連續範圍內的資訊。

示例1:查詢年齡在12到18歲的學生:

select * from student where name between 12 and 18;
示例2:查詢年齡不在12到18歲的學生:

select * from student where name age not  between 12 and 18;
五、 判斷是否為空

——is null/is not null

示例1:查詢資料表中姓名為空的記錄:

select * from student where name is null;
示例2:查詢資料表中年齡不為空的記錄:

select * from student where age is not null;

mysql 範圍查詢 Mysql 範圍查詢優化

1.2 另外,對於btree索引,index和乙個常量值通過 between,或者 操作符做比較 1.3 對於所有型別的index,多範圍條件通過 or and關鍵字組合形式 常量值 在之前的描述中意味著 2.1 查詢字串的常量形式 2.2 const 或者system表的一列 也只有一列 的自連線...

mysql基礎查詢和條件查詢

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

mysql查詢條件 Mysql查詢條件的使用

mysql查詢條件的使用 方法 解釋 gt 大於 gte 大於等於 lt 小於 lte 小於等於 例如 article article.objects.filter id gt 5 startswith 以指定某個字串開始,大小寫敏感 istartswith 以指定某個字串開始,大小寫不敏感 end...