MySQL學(xué)習(xí)必備條件查詢數(shù)據(jù)
一、條件查詢
利用where
語句可以對(duì)數(shù)據(jù)進(jìn)行篩選
select * from 表名 where 條件;
二、比較運(yùn)算符
? 運(yùn)算符 ? | ? 描述 ? | ? 例子 ? |
= | 等于 | where id = 1 |
\> | 大于 | where age > 10 |
< | 小于 | where age < 10 |
>= | 大于等于 | where age >= 10 |
<= | 小于等于 | where age <= 10 |
!= | 不等于 | where name != '老王' |
select * from users where id = 1;
三、邏輯運(yùn)算符
? 運(yùn)算符 ? | ? 描述 ? | ? 例子 ? |
and | 并且 | where id = 1 and age > 10 |
or | 或者 | where id = 1 or age > 10 |
not | 取反 | where not id = 1 |
select * from users where id = 1 and age = 24;
select * from users where not id = 1;
四、范圍查詢
? 運(yùn)算符 ? | ? 描述 ? | ? 例子 ? |
in | 在指定的非連續(xù)范圍內(nèi) | where id in(1,3,5); |
between ... and ... | 在指定的連續(xù)范圍內(nèi) | where id between 1 and 5; |
select * from users where id in (1,3,4);
select * from users where id between 1 and 5;
五、空判斷
? 運(yùn)算符 ? | ? 描述 ? | ? 例子 ? |
is null | 判斷是否為空 | where name is null |
is not null | 判斷是否不為空 | where name is not null |
注:null與''是不一樣的
INSERT INTO users (name, birth_date, phone,age) VALUES ('', '1990-01-01', '13813145213',30);
INSERT INTO users (name, birth_date, phone,age) VALUES (null, '1990-01-01', '13813145213',30);
INSERT INTO users (name, birth_date, phone,age) VALUES ('老張', null, '17813145213',30);
select * from users where birth_date is null;
六、模糊查詢
select * from users where name like '王%';
select * from users where name like '%王';
七、優(yōu)先級(jí)
- 小括號(hào),not,比較運(yùn)算符,邏輯運(yùn)算符
- and比or先運(yùn)算,如果同時(shí)出現(xiàn)并希望先算or,需要結(jié)合()使用
到此這篇關(guān)于MySQL學(xué)習(xí)必備條件查詢數(shù)據(jù)的文章就介紹到這了,更多相關(guān)MySQL條件查詢數(shù)據(jù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
升級(jí)到MySQL5.7后開發(fā)不得不注意的一些坑
這篇文章主要給大家介紹了關(guān)于升級(jí)到MySQL5.7后開發(fā)不得不注意的一些坑,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07mysql如果數(shù)據(jù)不存在,則插入新數(shù)據(jù),否則更新的實(shí)現(xiàn)方法
mysql如果數(shù)據(jù)不存在,則插入新數(shù)據(jù),否則更新的實(shí)現(xiàn)方法2011-11-11利用Xtrabackup工具備份及恢復(fù)(MySQL DBA的必備工具)
Xtrabackup 是percona的一個(gè)開源項(xiàng)目,可以熱備份innodb ,XtraDB,和MyISAM(會(huì)鎖表),可以看做是InnoDB Hotbackup的免費(fèi)替代品2013-04-04