索引

Using MySQL Regular Expressions

默认大小写不敏感

  1. mysql> SELECT '312HEWQKHD' REGEXP '[0-9]';
  2. +-----------------------------+
  3. | '312HEWQKHD' REGEXP '[0-9]' |
  4. +-----------------------------+
  5. | 1 |
  6. +-----------------------------+
  7. 1 row in set (0.00 sec)
  8.  
  9. mysql> SELECT 'HEWQKHD' REGEXP '[0-9]';
  10. +--------------------------+
  11. | 'HEWQKHD' REGEXP '[0-9]' |
  12. +--------------------------+
  13. | 0 |
  14. +--------------------------+
  15. 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.

  1. mysql> SELECT Concat(vend_name, ' (', vend_country, ')')
  2. -> FROM vendors
  3. -> ORDER BY vend_name;
  4. +--------------------------------------------+
  5. | Concat(vend_name, ' (', vend_country, ')') |
  6. +--------------------------------------------+
  7. | ACME (USA) |
  8. | Anvils R Us (USA) |
  9. | Furball Inc. (USA) |
  10. | Jet Set (England) |
  11. | Jouets Et Ours (France) |
  12. | LT Supplies (USA) |
  13. +--------------------------------------------+
  14. 6 rows in set (0.00 sec)

/

  1. SELECT Concat(RTrim(vend_name), ' (', RTrim(vend_country), ')')
  2. FROM vendors
  3. 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

  1. mysql> SELECT Concat(RTrim(vend_name), ' (', RTrim(vend_country), ')') AS
  2. -> vend_title
  3. -> FROM vendors
  4. -> ORDER BY vend_name;
  5. +-------------------------+
  6. | vend_title |
  7. +-------------------------+
  8. | ACME (USA) |
  9. | Anvils R Us (USA) |
  10. | Furball Inc. (USA) |
  11. | Jet Set (England) |
  12. | Jouets Et Ours (France) |
  13. | LT Supplies (USA) |
  14. +-------------------------+
  15. 6 rows in set (0.00 sec)

/

  1. SELECT prod_id,
  2. quantity,
  3. item_price,
  4. quantity*item_price AS expanded_price
  5. FROM orderitems
  6. 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

  1. 在 MySQL 中自定义函数是允许的。
  2. 允许在 WHERE SELECT INSERT .各种地方使用函数。
  3. 存在类似 Soundex 的比较有趣的函数,以及例如 IF 的方便函数。

官方 API → MySQL 5.7 Reference Manual  /  Functions and Operators

  1. mysql> SELECT cust_id, order_num, order_date
  2. -> FROM orders
  3. -> WHERE Date(order_date) = '2005-09-01';
  4. +---------+-----------+---------------------+
  5. | cust_id | order_num | order_date |
  6. +---------+-----------+---------------------+
  7. | 10001 | 20005 | 2005-09-01 00:00:00 |
  8. +---------+-----------+---------------------+
  9. 1 row in set (0.01 sec)
  1. mysql> SELECT cust_id, order_num, order_date
  2. -> FROM orders
  3. -> WHERE Year(order_date) = 2005 AND Month(order_date) = 9;
  4. +---------+-----------+---------------------+
  5. | cust_id | order_num | order_date |
  6. +---------+-----------+---------------------+
  7. | 10001 | 20005 | 2005-09-01 00:00:00 |
  8. | 10003 | 20006 | 2005-09-12 00:00:00 |
  9. | 10004 | 20007 | 2005-09-30 00:00:00 |
  10. +---------+-----------+---------------------+
  11. 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.

  1. mysql> SELECT COUNT(*) AS num_items,
  2. -> MIN(prod_price) AS price_min,
  3. -> MAX(prod_price) AS price_max,
  4. -> AVG(DISTINCT prod_price) AS price_avg
  5. -> FROM products;
  6. +-----------+-----------+-----------+-----------+
  7. | num_items | price_min | price_max | price_avg |
  8. +-----------+-----------+-----------+-----------+
  9. | 14 | 2.50 | 55.00 | 17.780833 |
  10. +-----------+-----------+-----------+-----------+
  11. 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的更多相关文章

  1. 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. ...

  2. 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 ...

  3. 剑指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 则依次打印 ...

  4. 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 ...

  5. MySQL Crash Course #13# Chapter 21. Creating and Manipulating Tables

    之前 manipulate 表里的数据,现在则是 manipulate 表本身. INDEX 创建多列构成的主键 自动增长的规定 查看上一次插入的自增 id 尽量用默认值替代 NULL 外键不可以跨引 ...

  6. MySQL Crash Course #06# Chapter 13. 14 GROUP BY. 子查询

    索引 理解 GROUP BY 过滤数据 vs. 过滤分组 GROUP BY 与 ORDER BY 之不成文的规定 子查询 vs. 联表查询 相关子查询和不相关子查询. 增量构造复杂查询 Always ...

  7. 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 ...

  8. MySQL Crash Course #02# Chapter 3. 4 通配符. 分页

    索引 查看表.文档操作 检索必须知道的两件事 数据演示由谁负责 通配符.非必要不用 检索不同的行 限制结果集.分页查找 运用数据库.表全名 命令后加分号对于很多 DBMS 都不是必要的,但是加了也没有 ...

  9. MySQL Crash Course #21# Chapter 29.30. Database Maintenance & Improving Performance

    终于结束这本书了,最后两章的内容在官方文档中都有详细介绍,简单过一遍.. 首先是数据备份,最简单直接的就是用 mysql 的内置工具 mysqldump MySQL 8.0 Reference Man ...

随机推荐

  1. 从Spring到SpringBoot构建WEB MVC核心配置详解

    目录 理解Spring WEB MVC架构的演变 认识Spring WEB MVC 传统时代的Spring WEB MVC 新时代Spring WEB MVC SpringBoot简化WEB MVC开 ...

  2. POJ-2329 Nearest number - 2(BFS)

    Nearest number - 2 Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 4100 Accepted: 1275 De ...

  3. Oracle体系结构之控制文件管理

    控制文件作用:记录了数据库的结构和行为,有多少个数据文件,日志文件及其位置名称,状态,维护数据库的一致性,即记录了数据库的启动SCN号和终止SCN号. 控制文件的位置和个数记录在参数文件中,通常控制文 ...

  4. 通过 Kubernetes 和容器实现 DevOps

    https://mp.weixin.qq.com/s/1WmwisSGrVyXixgCYzMA1w 直到 Docker 的出现(2008 年),容器才真正具备了较好的可操作性和实用性.容器技术的概念最 ...

  5. iOS开发tableView去掉顶部上部空表区域

    tableview中的第一个cell 里上部 有空白区域,大概64像素 在viewDidLoad中加入如下代码 self.automaticallyAdjustsScrollViewInsets = ...

  6. ie浏览器总跳转到 http://hao.360.cn

    起因在于  开启360某些防护之后,若出现使用ie无法打开网页的情况,那么就会跳转到http://hao.360.cn .把360的防护能关的都关掉,就不会跳转了. 第二个问题:chrome可以打开网 ...

  7. router-link params传参

    1.router.js配置 需要在路径后定义上要传的属性名 -->       /:属性名(query方式不需要) { path: '/CreateProgress/:name1', name: ...

  8. EControl平台测试向生产版本工程切换说明

    第一步,备份生产环境版本,假设生产环境版本工程名为SEHEControl,记录版本说明第二部,拷贝测试版本到新文件夹,假设测试版本工程名为SEHEControlTest第三步,进入工程文件夹,修改SL ...

  9. Linux输入输出重定向和文件查找值grep命令

    Linux输入输出重定向和文件查找值grep命令 一.文件描述符Linux 的shell命令,可以通过文件描述符来引用一些文件,通常使用到的文件描述符为0,1,2.Linux系统实际上有12个文件描述 ...

  10. [vue]计算和侦听属性(computed&watch)

    先看一下计算属性 vue只有data区的数据才具备响应式的功能. 计算和侦听属性 - v-text里可以写一些逻辑 <div id="example"> {{ mess ...