mysql常用操作彙總(三)

1.使用where過濾數據

select name,id,price from shopping where id='3';


MYSQL支持的其他操作符:


2.對於多個約束條件可以使用and或or進行連接,and和與相通,or與或相通:

select name,id,price from shopping where id='4' and price='444';


and和or一起使用,這裏要注意and的優先級比or要高:

如果沒有優先級之分,首先會匹配id爲3和5的兩組數據,然後篩選出price爲444的一組,但結果不是這樣的

select name,id,price from shopping where id='5'  or id='3' and price='444';

類似與:

select name,id,price from shopping where id='5'  or (id='3' and price='444');



注意:如果要把where和order by放在一起使用,where要在order by之前

select name,id,price from shopping where price='444' order by id;

3.使用通配符進行過濾

select name,id,price from shopping where price like '66';


4.使用通配符百分號(%)進行模糊匹配,百分號可以匹配一個字符串

select name,id,price from shopping where name like 'pic%';

select name,id,price from shopping where name like '%product%';

select name,id,price from shopping where price like '%6';


5.使用通配符下劃線(_)進行模糊匹配,下劃線只能匹配一個字符

select name,id,price from shopping where name like '_pic_product%';

select name,id,price from shopping where price like '_6';


注意:雖然通配符可以進行模糊匹配,但是儘量少用,1.因爲它的執行效率比較低,2.由於它是模糊匹配,可能會匹配到你不想要的數據

6.使用正則表達式進行匹配

select name,id,price from shopping where id regexp '[3-5].?';


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章