您好,登錄后才能下訂單哦!
#查詢的基本語法
select * from 表名;
from關(guān)鍵字后面寫表名,表示數(shù)據(jù)來源于是這張表
select后面寫表中的列名,如果是**表示在結(jié)果中顯示表中所有列
在select后面的列名部分,可以使用as為列起別名,這個(gè)別名出現(xiàn)在結(jié)果集中
如果要查詢多個(gè)列,之間使用逗號(hào)分隔
例:
select id,name from students;
消除重復(fù)行
在select后面列前使用distinct可以消除重復(fù)的行
select distinct gender from students;
#條件
使用where子句對表中的數(shù)據(jù)篩選,結(jié)果為true的行會(huì)出現(xiàn)在結(jié)果集中
語法如下:
select * from 表名 where 條件;
比較運(yùn)算符
等于=
大于>
大于等于>=
小于<
小于等于<=
不等于!=或<>
例:
查詢編號(hào)大于3的學(xué)生
select * from students where id>3;
查詢編號(hào)不大于4的科目
select * from subjects where id<=4;
查詢姓名不是“黃蓉”的學(xué)生
select * from students where sname!='黃蓉';
查詢沒被刪除的學(xué)生
select * from students where isdelete=0;
#邏輯運(yùn)算符
and
or
not
查詢編號(hào)大于3的女同學(xué)
select * from students where id>3 and gender=0;
查詢編號(hào)小于4或沒被刪除的學(xué)生
select * from students where id<4 or isdelete=0;
#模糊查詢
like
%表示任意多個(gè)任意字符
_表示一個(gè)任意字符
查詢姓黃的學(xué)生
select * from students where sname like "黃%";
查詢姓黃并且名字是一個(gè)字的學(xué)生
select * from students where sname like "黃_";
查詢姓黃并且名字是兩個(gè)字的學(xué)生
select * from students where sname like "黃__";
查詢姓黃或叫靖的學(xué)生
select * from students where name like "黃%" or sname like "%靖%";
查詢名字中有龍的學(xué)生
select * from students where name like "%龍%";
#范圍查詢
in表示在一個(gè)非連續(xù)的范圍內(nèi)
查詢編號(hào)是1或3或8的學(xué)生
select * from students where id in(1,3,8);
between ... and ...表示在一個(gè)連續(xù)的范圍內(nèi)
查詢學(xué)生是3至8的學(xué)生
select * from students where id between 3 and 8;
查詢學(xué)生是3至8的男生
select * from students where id between 3 and 8 and gender=1;
PS:當(dāng)一個(gè)查詢語句中有多個(gè)and時(shí),優(yōu)先匹配between and作為一組,where 語句后自左向右依次匹配,遇到一個(gè)between與之后面的and先匹配成一組,最后才是邏輯運(yùn)算符的and,邏輯運(yùn)算符的優(yōu)先級較低。
#空判斷
注意:null與''是不同的
判空is null
查詢沒有填寫地址的學(xué)生
select * from students where hometown is null;
判非空is not null
查詢填寫了地址的學(xué)生
select * from students where hometown is not null;
查詢填寫了地址的女生
select * from students where hometown is not null and gender=0;
#優(yōu)先級
小括號(hào),not,比較運(yùn)算符(與between同級),邏輯運(yùn)算符
and比or先運(yùn)算,如果同時(shí)出現(xiàn)并希望先算or,需要結(jié)合()使用
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。