玖叶教程网

前端编程开发入门

通用SQL函数及相关文档说明和例子

  • CONCAT(A, B) - 连接两个字符串值以创建单个字符串输出。通常用于将两个或多个字段合并为一个字段。
  • FORMAT(X, D)- 格式化数字 X 到 D 有效数字。
  • CURDATE(), CURTIME()- 返回当前日期或时间。
  • NOW() - 将当前日期和时间作为一个值返回。
  • MONTH()DAY()YEAR()WEEK()WEEKDAY() - 从日期值中提取给定数据。
  • HOUR()MINUTE()SECOND() - 从时间值中提取给定数据。
  • DATEDIFF(A,B) - 确定两个日期之间的差异,通常用于计算年龄
  • SUBTIME(A,B) - 确定两次之间的差异。
  • FROMDAYS(INT) 将整数天数转换为日期值。Given a day number N, returns a DATE value.


CONCAT(A, B) 的例子

CONCAT(A, B)

 CONCAT(str1,str2,...)

Returns the string that results from concatenating the arguments. May have one or more arguments. If all arguments are nonbinary strings, the result is a nonbinary string. If the arguments include any binary strings, the result is a binary string. A numeric argument is converted to its equivalent nonbinary string form.

CONCAT() returns NULL if any argument is NULL.

返回串联参数的结果的字符串。可以有一个或多个参数。如果所有的参数都是非二进制字符串,结果是一个非二进制字符串。如果参数包括任何二进制字符串,结果是一个二进制字符串。数字参数被转换为其等价的非二进制字符串形式。

如果任何参数是 NULL,CONCAT()返回 NULL

mysql> SELECT CONCAT('My', 'S', 'QL');
        -> 'MySQL'
mysql> SELECT CONCAT('My', NULL, 'QL');
        -> NULL
mysql> SELECT CONCAT(14.3);
        -> '14.3'

For quoted strings, concatenation can be performed by placing the strings next to each other:

对于带引号的字符串,可以通过将字符串放在彼此旁边来进行连接。

mysql> SELECT 'My' 'S' 'QL';
+-------+
| My    |
+-------+
| MySQL |
+-------+
1 row in set (0.00 sec)

FORMAT(X, D) 的例子

FORMAT(X, D)

 FORMAT(X,D[,locale])

Formats the number X to a format like '#,###,###.##', rounded to D decimal places, and returns the result as a string. If D is 0, the result has no decimal point or fractional part.

The optional third parameter enables a locale to be specified to be used for the result number's decimal point, thousands separator, and grouping between separators. Permissible locale values are the same as the legal values for the lc_time_names system variable (see Section 10.16, “MySQL Server Locale Support”). If no locale is specified, the default is 'en_US'.

将数字 X 转换成类似'#,##,##.##'的格式,四舍五入到小数点后的位置,并将结果作为一个字符串返回。如果 D 为 0,结果没有小数点或小数部分。

可选的第三个参数允许指定一个地区设置,用于结果数字的小数点、千位分隔符和分隔符之间的分组。允许的地区设置值与 lc_time_names 系统变量的合法值相同(见第 10.16 节,"MySQL 服务器地区支持")。如果没有指定 locale,默认为 "en_US"。

mysql> SELECT FORMAT(12332.123456, 4);
        -> '12,332.1235'
mysql> SELECT FORMAT(12332.1,4);
        -> '12,332.1000'
mysql> SELECT FORMAT(12332.2,0);
        -> '12,332'
mysql> SELECT FORMAT(12332.2,2,'de_DE');
        -> '12.332,20'

当前的日期或时间

  • CURDATE(), CURTIME()- 返回当前日期或时间。
  • NOW() - 将当前日期和时间作为一个值返回。
mysql> SELECT CURTIME();
+-----------+
| CURTIME() |
+-----------+
| 16:25:12  |
+-----------+
1 row in set (0.00 sec)

mysql> SELECT CURDATE();
+------------+
| CURDATE()  |
+------------+
| 2021-07-01 |
+------------+
1 row in set (0.00 sec)

mysql> SELECT NOW();
+---------------------+
| NOW()               |
+---------------------+
| 2021-07-01 16:25:51 |
+---------------------+
1 row in set (0.00 sec)

从日期值、时间值中提取

MONTH()DAY()YEAR()WEEK()WEEKDAY()

HOUR()MINUTE()SECOND()

mysql> select month(now());
+--------------+
| month(now()) |
+--------------+
|            7 |
+--------------+
1 row in set (0.00 sec)
mysql> select minute(now());
+---------------+
| minute(now()) |
+---------------+
|            21 |
+---------------+
1 row in set (0.00 sec)
# 临时的修改时区
mysql> set time_zone = '+8:00';
Query OK, 0 rows affected (0.00 sec)

mysql> select hour(now());
+-------------+
| hour(now()) |
+-------------+
|          16 |
+-------------+
1 row in set (0.00 sec)

DATEDIFF(A,B) 的例子

DATEDIFF(A,B) 确定两个日期之间的差异,通常用于计算年龄

DATEDIFF(expr1,expr2)

DATEDIFF() returns expr1 ? expr2 expressed as a value in days from one date to the other. expr1 and expr2 are date or date-and-time expressions. Only the date parts of the values are used in the calculation.

DATEDIFF()返回 expr1 - expr2,表达为从一个日期到另一个日期的天数。expr1 和 expr2 是日期或日期和时间表达式。在计算中只使用数值的日期部分。

mysql> SELECT DATEDIFF('2007-12-31 23:59:59','2007-12-30');
        -> 1
mysql> SELECT DATEDIFF('2010-11-30 23:59:59','2010-12-31');
        -> -31
mysql> select datediff('2021-07-01', '2001-02-03');
+--------------------------------------+
| datediff('2021-07-01', '2001-02-03') |
+--------------------------------------+
|                                 7453 |
+--------------------------------------+
1 row in set (0.00 sec)

比如你想看你的生日当天和今天的天数差(假如你是 2000 年 1 月 2 号的生日):

mysql> select datediff(now(), '2000-01-02');
+-------------------------------+
| datediff(now(), '2000-01-02') |
+-------------------------------+
|                          7851 |
+-------------------------------+
1 row in set (0.00 sec)

SUBTIME(A,B) 的例子

SUBTIME(A,B)

SUBTIME(expr1,expr2)

SUBTIME() returns expr1 ? expr2 expressed as a value in the same format as expr1. expr1 is a time or datetime expression, and expr2 is a time expression.

expr1 是一个时间或日期时间表达式,而 expr2 是一个时间表达式。

mysql> SELECT SUBTIME('2007-12-31 23:59:59.999999','1 1:1:1.000002');
        -> '2007-12-30 22:58:58.999997'
mysql> SELECT SUBTIME('01:00:00.999999', '02:00:00.999998');
        -> '-00:59:59.999999'

FROMDAYS(INT) 的例子

FROMDAYS(INT) 将整数天数转换为日期值。Given a day number N, returns a DATE value.

Use FROM_DAYS() with caution on old dates. It is not intended for use with values that precede the advent of the Gregorian calendar (1582).

对旧的日期谨慎使用 FROM_DAYS() 。它不打算用于公历(1582 年)出现之前的数值。

mysql> SELECT FROM_DAYS(730669);
+-------------------+
| FROM_DAYS(730669) |
+-------------------+
| 2000-07-03        |
+-------------------+
1 row in set (0.00 sec)
mysql> SELECT FROM_DAYS(366);
+----------------+
| FROM_DAYS(366) |
+----------------+
| 0001-01-01     |
+----------------+
1 row in set (0.00 sec)
mysql> SELECT FROM_DAYS(129600);
+-------------------+
| FROM_DAYS(129600) |
+-------------------+
| 0354-11-01        |
+-------------------+
1 row in set (0.00 sec)

发表评论:

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言