1. 程式人生 > >MySQL Crash Course #05# Chapter 9. 10. 11. 12 正則.函數. API

MySQL Crash Course #05# Chapter 9. 10. 11. 12 正則.函數. API

efficient sig direct pro sim aggregate HA 應用程序 則表達式

索引

  • 正則表達式: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

  1. 在 MySQL 中自定義函數是允許的。
  2. 允許在 WHERE SELECT INSERT .各種地方使用函數。
  3. 存在類似 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