mysql常用操作汇总(三)

来源:互联网 发布:arm linux 内核构建 编辑:程序博客网 时间:2024/04/27 10:55

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].?';


原创粉丝点击