MySQL中的查詢過濾條件where

2021-09-22 04:48:17 字數 4384 閱讀 3392

and,or,not

between and

別名表示式

首先建立乙個表並新增如下資料,在下述查詢中均用到此表

create table student(

id char(36) primary key,

name varchar(8) not null,

age int(3) default 0,

mobile char(11),

address varchar(150)

)insert into student values ('9b4435ec-372c-456a-b287-e3c5aa23dff4','張三',24,'12345678901','北京海淀');

insert into student values ('a273ea66-0a42-48d2-a17b-388a2feea244','李%四',10,'98765432130',null);

insert into student values ('eb0a220a-60ae-47b6-9e6d-a901da9fe355','張李三',11,'18338945560','安徽六安');

insert into student values ('6ab71673-9502-44ba-8db0-7f625f17a67d','王_五',28,'98765432130','北京朝陽區');

insert into student values ('0055d61c-eb51-4696-b2da-506e81c3f566','王_五%%',11,'13856901237','吉林省長春市寬平區');

%可以指代字元個數為0到n。

如下查詢語句

select * from student where name like '張%';
輸出結果為:%可以指代字元數1到n

再新增如下資料,然後查詢

insert into student value('0055d61c-eb51-4696-b2da-506e81c3f577','張',17,'13856901237','吉林省長春市寬平區')

select * from student where name like '張%';

輸出結果為:%也可以指代字元數0

select * from student where name like '%三';
輸出結果為

%位置可在前也可以在後,也可以單獨使用

select * from student where name like '%';

查出所有的資料,類似於萬用字元

怎麼樣查出含有%的資訊?如下語句可行嗎?

select * from student where name like '%%%'	;#會查出所有的,因為每個%都是萬用字元。不可行
用escape去掉%的特殊含義

select * from student where name like '%#%%' escape '#';

1,%#%%中#%是整體,去掉第二個%的特殊含義,第乙個和第三個不變,之後#%中%變為普通的字元,且#可換為字母和一些特殊字元,如#、$、,、\等字元。

2,escape後面單引號中只能是單個字元;

查詢結果如下,查出含有%號的資料

每個下劃線僅指代乙個字元

如下查詢語句

select * from student where name like '張_';
輸出結果如下,查不出表中name為 張 和 張李三的資料

下劃線(_)且僅指代乙個字元

如下查詢語句

select * from student where name like '___';	#三個下劃線
輸入結果為

select * from student where name like '張%' and age>10;
輸出結果為

select * from student where age=10 or age=11;
輸出年齡為10或者11的學生資訊

上述語句等價於

select * from student where age in (10,11)
注意:此處的(10,11)不為集合,只代表兩個值

驗證:如下查詢語句

select * from student where age in (10,24)
集合中含有年齡為17的,上述查詢語句的結果為

3,如果值為空,用如下語句查詢

select * from student where address is null
查詢結果如下

值不為空,用如下語句查詢

select * from student where address is not null
#小數在前,大數在後,且包括大數和小數

select * from student where age between 10 and 24
輸出結果為

select * from student where age between 24 and 10 #沒有結果
1,select所選字段後面可以指定別名以使查出來的結果所顯示的字段更好理解,欄位名與別名之間使用空格或as關鍵字間隔;為了閱讀方便,推薦使用as關鍵字這種方式定義字段別名。

增加別名語句:以address為例,adddress [as] addr, as可以省略

select address as addr from student
輸入結果為

2,在預設情況下,查詢結果中顯示的欄位名為大寫字母,如果別名中包含空格、特殊字元(例如%)或者對大小寫敏感,則別名需要使用雙引號引起來。

select age+1 "stu age" from student;

select length(name) as "name%length" from student;

如果不加雙引號,上述兩個sql語句無法執行

補充: 

select * from student #此語句查出所有的資訊,*為萬用字元,此法書寫只是為了方便,一般不使用*號,用下邊語句代替

select id,name,age,mobile,address from student

在查詢過程中可能用到表示式,例如年齡為int型別的,輸出age+1.語句如下

select age,age+1 from student;
輸出結果為

給age+1取別名有

select age,age+1 as new_age from student;
輸出結果中欄位名改為new_age,如下

numpy元素特定條件查詢過濾

a np.array 1,2,3,4,5 6,7,8,9,10 原始資料 e a 6 a 2 構造對原始資料進行篩選的條件 a4 np.where e,a,0 把滿足條件的選擇出來,原封不動的儲存,不滿足條件的元素置零 本質上,就是把矩陣元素,按照條件分類.a5 a e 把滿足條件的元素選擇出來,構...

ElasticSearch 常用的查詢過濾語句

query 和 filter 的區別請看 term主要用於精確匹配哪些值,比如數字,日期,布林值或 not analyzed 的字串 未經分析的文字資料型別 完整的例子,hostname 字段完全匹配成 saaap.wangpos.com 的資料 terms 跟 term 有點類似,但 terms ...

ElasticSearch 常用的查詢過濾語句

query 和 filter 的區別請看 term主要用於精確匹配哪些值,比如數字,日期,布林值或 not analyzed 的字串 未經分析的文字資料型別 完整的例子,hostname 字段完全匹配成 saaap.wangpos.com 的資料 terms 跟 term 有點類似,但 terms ...