运维的日记

mysql的高级匹配条件

2020-04-04  本文已影响0人  陈公子ks

1.模糊查询 like
_ 表示1一个字符
% 表示0-n个字符

mysql> select name from user where name like '_';
mysql> select name from user where name like '%a%';
mysql> select name from user where name like '%_%';

2.正则表达式 regexp
正则:^ $ [] * |

mysql> select name from user where name regexp '^a|t$';
mysql> select name from user where regexp '^[abc]';
mysql> select name from user where name regexp '^..$';

3.计算

mysql> select name,uid from user where uid%2=0;
mysql> update user set uid=uid+1 where id<=5;
mysql> select name,uid from user where id <=5;
mysql> update user set uid=uid-1 where id<=5;
mysql> select name,2020-age csnf from user where name="root";

4.查询结果在处理
avg 统计字段平均值
sum 统计字段之和
min 统计字段最小值
max 统计字段最大值
count 统计字段值的个数

mysql> select count(*) from user;
mysql> select count(name) from user;
mysql> select avg(uid) from user;
mysql> select min(gid) from user;

5.排序
order by 字段名
asc 升序 desc 降序

mysql> select name,uid from user where id<=15  order by uid;
mysql> select name,uid from user where id<=15 order by uid desc;

6.查询分组

mysql> select uid from user group by uid;

7.查询过滤 having

mysql> select name from user where uid > 5;
mysql> select name from user where uid > 5 having name="myslq";

8.限制查询行数 limit

mysql> select name,uid from user limit 3;
mysql> name,uid gid from user limit 3 3;
上一篇下一篇

猜你喜欢

热点阅读