本章包含最常用功能的列表,提供定义,说明和示例。
最常用的聚合函数如下 -
s.no | 名称与描述 |
---|---|
1 | count 它对记录总数进行统计。 示例 - select count(*)from customer_table; |
2 | min 它揭示了一组记录的最小值。 示例 - select组织,min(帐户)from合同group by组织; |
3 | max 它揭示了一组记录的最大值。 示例 - select组织,max(account_size)from合同group by组织; |
4 | avg 它计算一组的记录的平均值。 示例 -选择avg(account_size)from合同; |
5 | sum 它计算一组记录的总和。 示例 - select sum(account_size)from合同; |
timestampdiff函数提供了一种计算年龄 -
select curdate() as today; select id, dob, timestampdiff(year,dob,'2015-07-01') as age from officer_info;
concat函数在连接操作后返回结果字符串。 您可以使用一个或多个参数。 检查其语法如下 -
select concat(item, item,...);
查看以下示例 -
select concat('ram', 'bu', 'tan'); output:rambutan
以下是重要的日期函数 -
s.no | 名称与描述 |
---|---|
1 | curdate() 它以yyyy-mm-dd或yyyymmdd格式返回日期。 示例 - select curdate(); |
2 | date() 它以多种格式返回日期。 示例-create table product_release_tbl(x date); |
3 | curtime() 它以hh:mm:ss或hhmmss.uuuuuu格式返回时间。 示例 - select curtime(); |
4 | date_sub() 它从指定的日期增加或减少天数。 示例 - select date_sub('2016-02-08',interval 60 day); |
5 | datediff() 它确定两个日期之间的天数。 示例 - select datediff('2016-01-01 23:59:59','2016-01-03'); |
6 | date add() 它增加或减去日期和时间的任何时间单位。 示例 - select date_add('2016-01-04 23:59:59',interval 22 second); |
7 | extract() 它从日期中提取单位。 示例 - select extract(ypar from'2016-01-08'); |
8 | now() 它以yyyy-mm-dd hh:mm:ss或yyyymmddhhmmss.uuuuuu格式返回当前日期和时间。 示例 - select now(); |
9 | date format() 它根据指定的格式字符串格式化日期。 示例 - select date_format('2016-01-09 20:20:00','%w%m%y'); |
以下是一些重要的时间函数 -
它返回日期的秒数。s.no | 名称与描述 |
---|---|
1 | hour() 它返回时间的小时或已过去的小时数。 示例 - select hour('19:17:09'); |
2 | localtime() 它的作用就像now()。 |
3 | microsecond() 它返回时间的微秒。 示例 - select microsecond('16:30:00.543876'); |
4 | minute() 它返回时间的分钟。 示例 - select minute('2016-05-22 17:22:01'); |
5 |
second() 它返回日期的秒数。 示例 - select second('2016-03-12 16:30:04.000001'); |
6 | time_format() 它根据指定的格式字符串格式化时间。 示例 - select time_format('22:02:20','%h%k%h%i%l'); |
7 | timestamp() 它为活动提供了格式为yyyy-mm-dd hh:mm:dd的时间戳。 示例 - create table orders_(id int,tmst timestamp); |
下面给出了一些重要的数字函数在mariadb -
s.no | 名称与描述 |
---|---|
1 | truncate() 它返回一个截断的数字到小数位数指定。 示例 - select truncate(101.222,1); |
2 | cos() 它返回x弧度的余弦。 示例 - select cos(pi()); |
3 | ceiling() 它返回不小于x的最小整数。 示例 - select ceiling(2.11); |
4 | degrees() 它将弧度转换为度。 示例 - select degrees(pi()); |
5 | div() 它执行整数除法。 示例 - select 100 div 4; |
6 | exp() 它返回e的x的幂。 示例 - select exp(2); |
7 | floor() 它返回不超过x的最大整数。 示例 - select floor(2.01); |
8 | ln() 它返回x的自然对数。 示例 - select ln(3); |
9 | log() 它返回给定基数的自然对数或对数。 示例 - select log(3); |
10 | sqrt() 它返回平方根。 示例 - select sqrt(16); |
下面给出了重要的字符串函数 -
s.no | 名称与描述 |
---|---|
1 | instr() 它返回一个子串的第一个实例的位置。 示例 - select instr('rambutan','tan'); |
2 | right() 它返回最右边的字符串字符。 示例 - select right('rambutan',3); |
3 | length() 它返回字符串的字节长度。 示例 - select length('rambutan'); |
4 | locate() 它返回一个子串的第一个实例的位置。 示例 - select locate('tan','rambutan'); |
5 | insert() 它返回一个字符串,在某个位置有一个指定的子字符串,被修改。 示例 - select insert('ramputan',4,1,'b'); |
6 | left() 它返回最左边的字符。 示例 - select left('rambutan',3); |
7 | upper() 它将字符更改为大写。 示例 - select upper(lastname); |
8 | lower() 它将字符更改为小写。 示例 - select lower(lastname); |
9 | strcmp() 它比较字符串,当他们相等,则返回0。 示例 -选择strcmp('蛋','奶酪'); |
10 | replace() 它在替换字符后返回一个字符串。 示例 - select replace('sully','l','n'); |
11 | reverse() 它反转字符串中的字符。 示例 - select reverse('racecar'); |
12 | repeat() 它返回一个字符串,重复给定字符x次。 示例 - select repeat('ha',10); |
13 | substring() 它从字符串返回一个子串,从位置x开始。 示例 - select substring('rambutan',3); |
14 | trim() 它从字符串中删除尾随/前导字符。 示例 - select trim(leading'_'from'_rambutan'); |