每天进步一点点——常用SQL技巧
正则表达式的使用
正则表达式是指一个用来描述或者匹配一些列符合某个句法规则的字符串的单个字符串。
MySQL中利用REGEXP命令提供给用户扩展的正则表达式功能
正则表达式中的模式
序列 |
序列说明 |
^ |
在字符串的开始处进行匹配 |
$ |
在字符串的末尾出进行匹配 |
. |
匹配任意单个字符,包括换行符 |
[...] |
匹配出括号内的任意字符 |
[^...] |
匹配不出括号内的任意字符 |
a* |
匹配零个或者多个a(包括空串) |
a+ |
匹配1个或者多个a(不包括空串) |
a? |
匹配1个或者0个a |
a1|a2 |
匹配a1或者a2 |
a(m) |
匹配m个a |
a(m,) |
匹配m个或者更多个a |
a(m,n) |
匹配m到n个a |
a(,n) |
匹配0到n个a |
(.....) |
将元素组成单一元素 |
例子:
原表:
mysql> select * from t77;
+------------------+
|name |
+------------------+
|asdfgh |
|aaggsdfasf |
|sdfsadsfassfsf |
|asdfasdfasdfasdf |
|NULL |
|NULL |
+------------------+
6 rowsin set (0.00 sec)
在字符串的开始处进行匹配
mysql>select * from t77 where name regexp'^as';
+------------------+
| name |
+------------------+
| asdfgh |
| asdfasdfasdfasdf |
+------------------+
2 rows in set (0.00 sec)
在字符串的末尾出进行匹配
mysql>select * from t77 where name regexp'sf$';
+----------------+
| name |
+----------------+
| aaggsdfasf |
| sdfsadsfassfsf |
+----------------+
2 rows in set (0.00 sec)
匹配任意单个字符,包括换行符
mysql>select * from t77 where name regexp'.a';
+------------------+
| name |
+------------------+
| aaggsdfasf |
| sdfsadsfassfsf |
| asdfasdfasdfasdf |
+------------------+
3 rows in set (0.00 sec)
mysql>select * from t77 where name regexp 'a';
+------------------+
| name |
+------------------+
| asdfgh |
| aaggsdfasf |
| sdfsadsfassfsf |
| asdfasdfasdfasdf |
+------------------+
4 rows in set (0.00 sec)
可以看出,如果使用.a则a前面必须有至少一个字符才会被匹配。
匹配出括号内的任意字符
mysql>select * from t77 where name regexp'gop';
Empty set (0.00 sec)
mysql>select * from t77 where name regexp'[gop]';
+------------+
| name |
+------------+
| asdfgh |
| aaggsdfasf |
+------------+
2 rows in set (0.00 sec)
可以看出,只要包含[]里的任意一个字符即可被筛选出来。
匹配不出括号内的任意字符
mysql>select * from t77 where name regexp'[^asdfh]';
+------------+
| name |
+------------+
| asdfgh |
| aaggsdfasf |
+------------+
2 rows in set (0.00 sec)
mysql>select * from t77 where name regexp'[^asdfg]';
+--------+
| name |
+--------+
| asdfgh |
+--------+
1 row in set (0.00 sec)
由此可以看出,利用这个正则表达式,能够筛选不出括号的任意字符,与[....]相反,也就是说条件的元素与筛选内容相交,筛选内容必有多余元素,也就是说筛选出来的内容必然不是条件的子集。
匹配零个或者多个a(包括空串)
mysql>select * from t77 where name regexp 'gg*';
+------------+
| name |
+------------+
| asdfgh |
| aaggsdfasf |
+------------+
2 rows in set (0.00 sec)
匹配1个或者多个a(不包括空串)
mysql>select * from t77 where name regexp 'gg+';
+------------+
| name |
+------------+
| aaggsdfasf |
+------------+
1 row in set (0.00 sec)
匹配1个或者0个a
mysql>select * from t77 where name regexp 'gg?';
+------------+
| name |
+------------+
| asdfgh |
| aaggsdfasf |
+------------+
2 rows in set (0.00 sec)
匹配a1或者a2
mysql>select * from t77 where name regexp 'ss|gh';
+----------------+
| name |
+----------------+
| asdfgh |
| sdfsadsfassfsf |
+----------------+
2 rows in set (0.00 sec)
巧用RAND()提取随机行
mysql>select * from t9 order by rand() limit 3;
+---------+----------------------------------+
| id | name |
+---------+----------------------------------+
| 1750322 |tcpdump:x:72:72::/:/sbin/nologin |
| 1287049 | bageyalu |
| 1301398 |bin:x:1:1:bin:/bin:/sbin/nologin |
+---------+----------------------------------+
通过rand()来获取随机行
利用GROUP BY的WITH ROLLUP子句
mysql>select date_format(payment_date,'%Y-%m'),staff_id,sum(amount) from paymentgroup by date_format(payment_date,'%Y-%m'),staff_id;
+-----------------------------------+----------+-------------+
| date_format(payment_date,'%Y-%m') |staff_id | sum(amount) |
+-----------------------------------+----------+-------------+
| 2005-05 | 1 | 2621.83 |
| 2005-05 | 2 | 2202.60 |
| 2005-06 | 1 | 4776.36 |
| 2005-06 | 2 | 4855.52 |
| 2005-07 | 1 | 14003.54 |
| 2005-07 | 2 | 14370.35 |
| 2005-08 | 1 | 11853.65 |
| 2005-08 | 2 | 12218.48 |
| 2006-02 | 1 | 234.09 |
| 2006-02 | 2 | 280.09 |
+-----------------------------------+----------+-------------+
10 rows in set (0.08 sec)
mysql>select date_format(payment_date,'%Y-%m'),staff_id,sum(amount) from paymentgroup by date_format(payment_date,'%Y-%m'),staff_id with rollup;
+-----------------------------------+----------+-------------+
| date_format(payment_date,'%Y-%m') |staff_id | sum(amount) |
+-----------------------------------+----------+-------------+
| 2005-05 | 1 | 2621.83 |
| 2005-05 | 2 | 2202.60 |
|2005-05 | NULL | 4824.43 |
| 2005-06 | 1 | 4776.36 |
| 2005-06 | 2 | 4855.52 |
|2005-06 | NULL | 9631.88 |
| 2005-07 | 1 | 14003.54 |
| 2005-07 | 2 | 14370.35 |
|2005-07 | NULL | 28373.89 |
| 2005-08 | 1 | 11853.65 |
| 2005-08 | 2 | 12218.48 |
|2005-08 | NULL | 24072.13 |
| 2006-02 | 1 | 234.09 |
| 2006-02 | 2 | 280.09 |
|2006-02 | NULL | 514.18 |
|NULL | NULL| 67416.51 |
+-----------------------------------+----------+-------------+
16 rows in set (0.06 sec)
通过上面例子可以看出来,下面的查询在每年都计算出来全年的总支持金额,并且在最后也算出了总的支出金额,。其实with rollup反映的是一种OLAP思想,也就是说这个GROUP BY语句执行完成后可以满足用户想要得到的任何一个分组及分组组合的聚合信息值。但是rollup和order by是互斥的,并且limit应该用在rollup之后。
利用BIT GROUP FUNCTIONS做统计
BIT_AND()
按位与,有1则为1
mysql>create table order_rab(id int,customer_id int,kind int);
Query OK, 0 rows affected (0.02 sec)
mysql>insert into order_rab values(1,1,5),(2,1,4);
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql>insert into order_rab values(3,2,3),(4,2,4);
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql>select * from rab;
ERROR 1146 (42S02): Table 'test2.rab'doesn't exist
mysql>select * from order_rab;
+------+-------------+------+
| id | customer_id | kind |
+------+-------------+------+
| 1 | 1 | 5 |
| 2 | 1 | 4 |
| 3 | 2 | 3 |
| 4 | 2 | 4 |
+------+-------------+------+
4 rows in set (0.00 sec)
mysql>select customer_id,bit_or(kind) from order_rab group by customer_id;
+-------------+--------------+
| customer_id | bit_or(kind) |
+-------------+--------------+
| 1 | 5 |
| 2 | 7 |
+-------------+--------------+
2 rows in set (0.00 sec)
上面例子,是5和4做或运算=(0101|0100)=0101=5
3和4做或运算=(0011|0100)=0111=7
BIT_OR()
按位与,同为1则为1
mysql>select customer_id,bit_and(kind) from order_rab group by customer_id;
+-------------+---------------+
| customer_id | bit_and(kind) |
+-------------+---------------+
| 1 | 4 |
| 2 | 0 |
+-------------+---------------+
2 rows in set (0.00 sec)
上面例子,是5和4做与运算=(0101&0100)=0100=4
3和4做或运算=(0011&0100)=0000=0
使用外键要注意的问题
由于MyISAM表不支持外键,所以在使用外键的时候一定不要使用不支持外键的表,并且可以通过show create table来检查是否可以使用外键。
每天进步一点点——常用SQL技巧相关推荐
- mysql高级篇三:常用sql技巧
文章目录 SQL执行顺序 正则表达式使用 MySQL 常用函数 SQL执行顺序 编写顺序 SELECT DISTINCT<select list> FROM<left_table&g ...
- Oracle 常用SQL技巧收藏
1. SELECT子句中避免使用 "*" 当你想在SELECT子句中列出所有的COLUMN时,使用动态SQL列引用 '*' 是一个方便的方法.不幸的是,这是一个非常低效的方 ...
- 常用 SQL 技巧和常见问题
1.正则表达式的使用 正则表达式(Regular Expression),是指一个用来描述或者匹配一系列符合某个句法规则的字符串的单个字符串. MySQL 利用 REGEXP 命令提供给用户扩展的正则 ...
- idea 分栏_常用SQL优化与IDEA超实用技巧分享
原标题:常用SQL优化与IDEA超实用技巧分享 今天,数据库的操作越来越成为整个应用的性能瓶颈了,这点对于Web应用尤其明显.关于数据库的性能,这并不只是DBA才需要担心的事,更是我们程序员需要去关注 ...
- 学习ArcGIS?37个Arcmap常用操作技巧可能帮到您
概述 ARCgis软件作为地信人日常工作绕不开的软件之一,产品功能相对来说比较复杂,对于新手来说从入门到完全上手需要很长时间学习,这里就为大家整理了37个个软件常用操作技巧,希望能够帮到需要的同行(文 ...
- VC++ 常用编程技巧总结
VC++6.0的使用以及编程技巧整理 1.MDI子窗口一启动就最大化 BOOL CChildFrame::PreCreateWindow(CREATESTRUCT& cs) ...
- mysql去重汇总_Mysql常用SQL汇总
今天的博客主题 数据库篇 -->Mysql -->Mysql常用SQL汇总 目录 字段合并去重 查询结果追加自定义字符串 查询当天的数据 字段合并去重 今天工作当中被同事问道数据库需要两个 ...
- sqlserver数据库操作大全——常用语句/技巧集锦/经典语句
☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆ ☆ ☆ ☆ sql 宝 典 ☆ ☆ ☆ 2012年-8月 修订版 ☆ ☆ 修订人:情谊续写经典 ☆ ☆ 郑重 ...
- 测试常用 SQL 命令
对"全方位"测试人员的需求正在增长."全方位"是什么意思?这意味着测试人员除了具备传统的"点点点"测试方法来测试系统功能的能力,并能够利用技 ...
最新文章
- 11.5. blockquote
- 如何用 Nacos 构建服务网格生态?
- 关于汽车领域的知识图谱实战入门
- c++ 如何确认网卡为无线网卡_笔记本无线网卡有哪几种 有必要升级吗
- Nginx学习之十三-负载均衡-IP哈希策略剖析
- exe文件注册服务的方法
- sql 如何把查询得到的结果如何放入一个新表中
- 使用JSON作为函数的参数(转载)
- mysql的驱动connect放在哪里_十年测试解析:ddt结合excel,csv,mysql实现自动化测试数据驱动...
- flex布局——回顾
- 拒绝外部投资、融资及收购,Epic是如何成为美国电子病历“一姐”的?
- 学习java之前应该先了解哪些知识?
- 有用的函数-系统采集(一)
- 万字长文带你轻松了解LSTM全貌
- DHCP-relay测试总结
- 次要和主要等位基因的定义
- vue页面自适应屏幕宽高_vue组件页面高度根据屏幕大小自适应
- 你的Web3域名,价值究竟何在?
- tiny4412 裸机程序 七、重定位代码到DRAM【转】
- 【SPSS】数据预处理基础教程(附案例实战)