MySQL Crash Course #05# Chapter 9. 10. 11. 12 正则.函数. API
索引
- 正则表达式:MySQL only supports a small subset of what is supported in most regular expression implementations
- 计算字段,应用程序计算 VS. 数据库计算,Concat. 假名 + - * /
- 测试 算数. 函数表达式
- MySQL 函数. API
- 聚集函数示例
Using MySQL Regular Expressions
→ 默认大小写不敏感
mysql> SELECT '312HEWQKHD' REGEXP '[0-9]';
+-----------------------------+
| '312HEWQKHD' REGEXP '[0-9]' |
+-----------------------------+
| 1 |
+-----------------------------+
1 row in set (0.00 sec) mysql> SELECT 'HEWQKHD' REGEXP '[0-9]';
+--------------------------+
| 'HEWQKHD' REGEXP '[0-9]' |
+--------------------------+
| 0 |
+--------------------------+
1 row in set (0.00 sec)
测试 正则表达式并不需要一张特定的表 ↑ REGEXP checks always return 0 (not a match) or 1 (match).
正常而言,LIKE 能做的事情正则一定能做,正则能做的 LIKE 不一定能做(例如说 ' x | x '),效率上我倾向于正则比较慢(因为比较繁琐,匹配的东西多),但是实际情况还是要试了才知道。
Understanding Calculated Fields
ps. Fields 和 column 通常指一个东西
Rather than retrieve the data as it is and then reformat it within your client application or report, what you really want is to retrieve converted, calculated, or reformatted data directly from the database.
This is where calculated fields come in. Unlike all the columns we retrieved in the chapters thus far, calculated fields don't actually exist in database tables. Rather, a calculated field is created on-the-fly within a SQL SELECT statement.
Many of the conversions and reformatting that can be performed within SQL statements can also be performed directly in your client application. However, as a rule, it is far quicker to perform these operations on the database server than it is to perform them within the client because Database Management Systems (DBMS) are built to perform this type of processing quickly and efficiently.
/
In MySQL SELECT statements, you can concatenate columns using the Concat() function.
MySQL Is Different Most DBMSs use operators + or || for concatenation; MySQL uses the Concat() function. Keep this in mind when converting SQL statements to MySQL.
mysql> SELECT Concat(vend_name, ' (', vend_country, ')')
-> FROM vendors
-> ORDER BY vend_name;
+--------------------------------------------+
| Concat(vend_name, ' (', vend_country, ')') |
+--------------------------------------------+
| ACME (USA) |
| Anvils R Us (USA) |
| Furball Inc. (USA) |
| Jet Set (England) |
| Jouets Et Ours (France) |
| LT Supplies (USA) |
+--------------------------------------------+
6 rows in set (0.00 sec)
/
SELECT Concat(RTrim(vend_name), ' (', RTrim(vend_country), ')')
FROM vendors
ORDER BY vend_name;
The trim() Functions In addition to RTrim() (which, as just seen, trims the right side of a string), MySQL supports the use of LTrim() (which trims the left side of a string), and trim() (which trims both the right and left).
/
Using Aliases
mysql> SELECT Concat(RTrim(vend_name), ' (', RTrim(vend_country), ')') AS
-> vend_title
-> FROM vendors
-> ORDER BY vend_name;
+-------------------------+
| vend_title |
+-------------------------+
| ACME (USA) |
| Anvils R Us (USA) |
| Furball Inc. (USA) |
| Jet Set (England) |
| Jouets Et Ours (France) |
| LT Supplies (USA) |
+-------------------------+
6 rows in set (0.00 sec)
/
SELECT prod_id,
quantity,
item_price,
quantity*item_price AS expanded_price
FROM orderitems
WHERE order_num = 20005;
How to Test Calculations
SELECT provides a great way to test and experiment with functions and calculations. Although SELECT is usually used to retrieve data from a table, the FROM clause may be omitted to simply access and work with expressions. For example, SELECT 3 * 2; would return 6, SELECT Trim(' abc '); would return abc, and SELECT Now() uses the Now() function to return the current date and time. You get the ideause SELECT to experiment as needed.
Using Functions
- 在 MySQL 中自定义函数是允许的。
- 允许在 WHERE SELECT INSERT 等.各种地方使用函数。
- 存在类似 Soundex 的比较有趣的函数,以及例如 IF 的方便函数。
官方 API → MySQL 5.7 Reference Manual / Functions and Operators
mysql> SELECT cust_id, order_num, order_date
-> FROM orders
-> WHERE Date(order_date) = '2005-09-01';
+---------+-----------+---------------------+
| cust_id | order_num | order_date |
+---------+-----------+---------------------+
| 10001 | 20005 | 2005-09-01 00:00:00 |
+---------+-----------+---------------------+
1 row in set (0.01 sec)
mysql> SELECT cust_id, order_num, order_date
-> FROM orders
-> WHERE Year(order_date) = 2005 AND Month(order_date) = 9;
+---------+-----------+---------------------+
| cust_id | order_num | order_date |
+---------+-----------+---------------------+
| 10001 | 20005 | 2005-09-01 00:00:00 |
| 10003 | 20006 | 2005-09-12 00:00:00 |
| 10004 | 20007 | 2005-09-30 00:00:00 |
+---------+-----------+---------------------+
3 rows in set (0.00 sec)
在处理时间相关的问题时可能会比较有用。
Using Aggregate Functions
Aggregate Functions Functions that operate on a set of rows to calculate and return a single value.
mysql> SELECT COUNT(*) AS num_items,
-> MIN(prod_price) AS price_min,
-> MAX(prod_price) AS price_max,
-> AVG(DISTINCT prod_price) AS price_avg
-> FROM products;
+-----------+-----------+-----------+-----------+
| num_items | price_min | price_max | price_avg |
+-----------+-----------+-----------+-----------+
| 14 | 2.50 | 55.00 | 17.780833 |
+-----------+-----------+-----------+-----------+
1 row in set (0.00 sec)
Aggregate functions are used to summarize data. MySQL supports a range of aggregate functions, all of which can be used in multiple ways to return just the results you need. These functions are designed to be highly efficient, and they usually return results far more quickly than you could calculate them yourself within your own client application.
MySQL Crash Course #05# Chapter 9. 10. 11. 12 正则.函数. API的更多相关文章
- atitit.Oracle 9 10 11 12新特性attilax总结
atitit.Oracle 9 10 11 12新特性 1. ORACLE 11G新特性 1 1.1. oracle11G新特性 1 1.2. 审计 1 1.3. 1. 审计简介 1 1.4. ...
- Java面试题:n=2\n1*2*5*6\n--3*4\n\nn=3\n1*2*3*10*11*12\n--4*5*8*9\n----6*7\n如何实现如上结构的数据
今天学长在面试的时候遇到了一道题,然后让大家做一做. 在不看下面的答案之前,悠闲的朋友们一起来抖动一下大脑吧! 以下是我的想法: import java.util.Scanner;public cla ...
- 剑指offer19:按照从外向里以顺时针的顺序依次打印出每一个数字,4 X 4矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数字1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10.
1 题目描述 输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下4 X 4矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印 ...
- win7 PLSQL Developer 10/11/12 连接 Oracle 10/11/12 x64位数据库配置详解(与32位一样,只要注意对应Oracle Instant Client版本) tns 错误和 nls错误
环境win7 x64 PLSQL Developer 10 与 11 Oracle Instant Client 10 与 12 参考http://blog.csdn.net/chen_zw/arti ...
- MySQL Crash Course #13# Chapter 21. Creating and Manipulating Tables
之前 manipulate 表里的数据,现在则是 manipulate 表本身. INDEX 创建多列构成的主键 自动增长的规定 查看上一次插入的自增 id 尽量用默认值替代 NULL 外键不可以跨引 ...
- MySQL Crash Course #06# Chapter 13. 14 GROUP BY. 子查询
索引 理解 GROUP BY 过滤数据 vs. 过滤分组 GROUP BY 与 ORDER BY 之不成文的规定 子查询 vs. 联表查询 相关子查询和不相关子查询. 增量构造复杂查询 Always ...
- MySQL Crash Course #04# Chapter 7. 8 AND. OR. IN. NOT. LIKE
索引 AND. OR 运算顺序 IN Operator VS. OR NOT 在 MySQL 中的表现 LIKE 之注意事项 运用通配符的技巧 Understanding Order of Evalu ...
- MySQL Crash Course #02# Chapter 3. 4 通配符. 分页
索引 查看表.文档操作 检索必须知道的两件事 数据演示由谁负责 通配符.非必要不用 检索不同的行 限制结果集.分页查找 运用数据库.表全名 命令后加分号对于很多 DBMS 都不是必要的,但是加了也没有 ...
- MySQL Crash Course #21# Chapter 29.30. Database Maintenance & Improving Performance
终于结束这本书了,最后两章的内容在官方文档中都有详细介绍,简单过一遍.. 首先是数据备份,最简单直接的就是用 mysql 的内置工具 mysqldump MySQL 8.0 Reference Man ...
随机推荐
- Navicat 同步数据库中数据
Navicat工具同步两个数据库中的数据 第一步在我们的电脑里面打开navicat软件,打开要复制表的数据库,如下图所示: 第二步点击上方的“工具->数据传输”,如下图所示: 第三步进 ...
- 江南OJ 1151 - 还是晒太阳 - [状压DP]
题目链接:校内OJ的题目,就不放链接了. PS.可以说是本次9月月赛唯一的一道有一定难度的题目了. 题解: 考虑状压DP,假设 $sta$ 是一个二进制数,代表当前 $n$ 个人有几个是在队伍里的,剩 ...
- Django-MySQL数据库使用01
Django连接数据库的要求:1)Pycharm运行Django平台:2)MySQL数据库.本文的前提是这两个平台读者都已经都正确安装,未安装的朋友请自行百度.说明一下我用的Django是2.1版本, ...
- 【python基础】python程序打包成.exe运行时会弹出黑框
用python调用.bat或者.exe文件时,一般调用 方式如下: os.system("C:\Windows\System32\osk.exe") 对吧,这样就会因为调用了系统s ...
- 事务控制及try catch
一.事务控制 BEGIN TRY BEGIN TRAN; DECLARE @aaa NVARCHAR(MAX); SET @aaa = 9 / 0; COMMIT TRAN;END TRYBEGIN ...
- sql server 备份恢复效率
sql server 备份恢复效率 如何提高备份的速度呢? 其实这个问题和如何让系统跑的更快是一样的,要想系统跑的更快,无非就是:优化系统,或者就是更好更强大的服务器,特别是更多的cpu.更大的内存. ...
- glide从入门到精通使用
介绍 不论是开发Java还是你正在学习的Golang,都会遇到依赖管理问题.Java有牛逼轰轰的Maven和Gradle. Golang亦有godep.govendor.glide.gvt.gopac ...
- abap 基本知识
sap gui 安装教程:http://www.itpub.net/forum.php?mod=viewthread&tid=2090890 1:abap 基本数据类型: i(整型),f(浮点 ...
- 多张图片合成一个tif
可以利用ACDSEE6.0打开你要合成的多张图片,CTRL全部选中,打开工具--转化文件格式-选择格式tif---所有页----合并---
- c#实现图片二值化例子(黑白效果)
C#将图片2值化示例代码,原图及二值化后的图片如下: 原图: 二值化后的图像: 实现代码: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 2 ...