字符函数

length 获取字节量

## 查看英文的字节长度

mysql> select length('haha');+----------------+

| length('haha') |

+----------------+

| 4 |

+----------------+

1 row in set (0.00sec)

## 查看中文的字节长度

mysql> select length('哈哈');+------------------+

| length('哈哈') |

+------------------+

| 6 |

+------------------+

1 row in set (0.00sec)

## 查看表中某一列的字节长度

mysql> select length(first_name) as len fromemployees group by len ;+-----+

| len |

+-----+

| 6 |

| 7 |

| 5 |

| 9 |

| 4 |

| 8 |

| 10 |

| 3 |

| 11 |

| 12 |

| 13 |

| 14 |

+-----+

12 rows in set (1.65 sec)

查看emoji表情的字节长度

concat函数  拼接字符串

mysql> select concat("我是","中国","人");+---------------------------------+

| concat("我是","中国","人") |

+---------------------------------+

| 我是中国人 |

+---------------------------------+

1 row in set (0.00sec)

mysql> select concat(first_name ,"的生日是:",birth_date) from employees limit 10;+----------------------------------------------------+

| concat(first_name ,"的生日是:",birth_date) |

+----------------------------------------------------+

| Georgi 的生日是: 1953-09-02 |

| Bezalel 的生日是: 1964-06-02 |

| Parto 的生日是: 1959-12-03 |

| Chirstian 的生日是: 1954-05-01 |

| Kyoichi 的生日是: 1955-01-21 |

| Anneke 的生日是: 1953-04-20 |

| Tzvetan 的生日是: 1957-05-23 |

| Saniya 的生日是: 1958-02-19 |

| Sumant 的生日是: 1952-04-19 |

| Duangkaew 的生日是: 1963-06-01 |

+----------------------------------------------------+

10 rows in set (0.00 sec)

备份数据库语句的拼接

mysql> select concat("mysqldump -uroot -p123",table_schema," ",table_name,"> /bak/",table_schema,"_",table_name,".sql") from informatiion_schema.tables where table_schema='world';+-------------------------------------------------------------------------------------------------------------+

| concat("mysqldump -uroot -p123",table_schema," ",table_name,"> /bak/",table_schema,"_",table_name,".sql") |

+-------------------------------------------------------------------------------------------------------------+

| mysqldump -uroot -p123 world City > /bak/world_City.sql |

| mysqldump -uroot -p123 world Country > /bak/world_Country.sql |

| mysqldump -uroot -p123 world CountryLanguage > /bak/world_CountryLanguage.sql |

+-------------------------------------------------------------------------------------------------------------+

3 rows in set (0.00 sec)

upper && lower 大小写转换

mysql> select first_name from employees limit 10;+------------+

| first_name |

+------------+

| Georgi |

| Bezalel |

| Parto |

| Chirstian |

| Kyoichi |

| Anneke |

| Tzvetan |

| Saniya |

| Sumant |

| Duangkaew |

+------------+

10 rows in set (0.00sec)

mysql> select upper(first_name) from employees limit 10;+-------------------+

| upper(first_name) |

+-------------------+

| GEORGI |

| BEZALEL |

| PARTO |

| CHIRSTIAN |

| KYOICHI |

| ANNEKE |

| TZVETAN |

| SANIYA |

| SUMANT |

| DUANGKAEW |

+-------------------+

10 rows in set (0.11sec)

mysql> select lower(first_name) from employees limit 10;+-------------------+

| lower(first_name) |

+-------------------+

| georgi |

| bezalel |

| parto |

| chirstian |

| kyoichi |

| anneke |

| tzvetan |

| saniya |

| sumant |

| duangkaew |

+-------------------+

10 rows in set (0.00 sec)

substr 截取字符串

mysql> select substr(birth_date,1,4) year from employees limit 10;+------+

| year |

+------+

| 1953 |

| 1964 |

| 1959 |

| 1954 |

| 1955 |

| 1953 |

| 1957 |

| 1958 |

| 1952 |

| 1963 |

+------+

10 rows in set (0.00sec)

mysql> select substr(birth_date,6) date from employees limit 10;+-------+

| date |

+-------+

| 09-02 |

| 06-02 |

| 12-03 |

| 05-01 |

| 01-21 |

| 04-20 |

| 05-23 |

| 02-19 |

| 04-19 |

| 06-01 |

+-------+

10 rows in set (0.00 sec)

instr 返回字符串首次出现的索引,没有找到就返回0

mysql> select instr(birth_date,'80') from employees limit 10;+------------------------+

| instr(birth_date,'80') |

+------------------------+

| 0 |

| 0 |

| 0 |

| 0 |

| 0 |

| 0 |

| 0 |

| 0 |

| 0 |

| 0 |

+------------------------+

10 rows in set (0.00sec)

mysql> select instr(birth_date,'19') from employees limit 10;+------------------------+

| instr(birth_date,'19') |

+------------------------+

| 1 |

| 1 |

| 1 |

| 1 |

| 1 |

| 1 |

| 1 |

| 1 |

| 1 |

| 1 |

+------------------------+

10 rows in set (0.00 sec)

mysql> select id,instr(name,"qingdao") as a from City where CountryCode ='CHN' having a>0;

+------+---+

| id | a |

+------+---+

| 1903 | 1 |

+------+---+

1 row in set (0.01 sec)

trim 去掉行首和行尾的指定字符,默认为空格

mysql> select trim('hello' from 'hello world') astest;+--------+

| test |

+--------+

| world |

+--------+

1 row in set (0.00sec)

mysql> select trim('hello' from 'hello world') astest;+--------------+

| test |

+--------------+

| hello world |

+--------------+

1 row in set (0.00sec)

mysql> select trim('world' from 'hello world') astest;+---------+

| test |

+---------+

| hello |

+---------+

1 row in set (0.00sec)

mysql> select trim('world' from 'hello world') astest;+---------------+

| test |

+---------------+

| hello world |

+---------------+

1 row in set (0.00 sec)

Lpad 左填充

mysql> select concat(lpad(floor(rand()*24),2,0),':',lpad(floor(rand()*60),2,0),':',lpad(floor(rand()*60),2,0))d ;+----------+

| d |

+----------+

| 05:00:23 |

+----------+

1 row in set (0.00sec)

mysql> select concat(lpad(floor(rand()*24),2,0),':',lpad(floor(rand()*60),2,0),':',lpad(floor(rand()*60),2,0))d ;+----------+

| d |

+----------+

| 22:33:54 |

+----------+

1 row in set (0.00 sec)

rpad 右侧填充

replace 替换字符串

mysql> selectuuid();+--------------------------------------+

| uuid() |

+--------------------------------------+

| 5a87e51c-aac4-11ea-b4fc-000c295e277d |

+--------------------------------------+

1 row in set (0.01sec)

mysql> select replace(uuid(),'-','');+----------------------------------+

| replace(uuid(),'-','') |

+----------------------------------+

| bc8c03aeaac411eab4fc000c295e277d |

+----------------------------------+

1 row in set (0.00 sec)

数学函数

round 四舍五入

mysql> select round(10.105);+---------------+

| round(10.105) |

+---------------+

| 10 |

+---------------+

1 row in set (0.00sec)

mysql> select round(10.10569,3);+-------------------+

| round(10.10569,3) |

+-------------------+

| 10.106 |

+-------------------+

1 row in set (0.00 sec)

ceil 向上取整

mysql> select ceil(-3.12);+-------------+

| ceil(-3.12) |

+-------------+

| -3 |

+-------------+

1 row in set (0.00sec)

mysql> select ceil(3.12);+------------+

| ceil(3.12) |

+------------+

| 4 |

+------------+

1 row in set (0.00sec)

mysql> select ceil(3.00);+------------+

| ceil(3.00) |

+------------+

| 3 |

+------------+

1 row in set (0.00 sec)

floor 向下取整

mysql> select floor(3.00);+-------------+

| floor(3.00) |

+-------------+

| 3 |

+-------------+

1 row in set (0.00sec)

mysql> select floor(3.12);+-------------+

| floor(3.12) |

+-------------+

| 3 |

+-------------+

1 row in set (0.00sec)

mysql> select floor(-3.12);+--------------+

| floor(-3.12) |

+--------------+

| -4 |

+--------------+

1 row in set (0.00 sec)

truncate 截取浮点数小数点后的位数

mysql> select truncate(3.1415,3);+--------------------+

| truncate(3.1415,3) |

+--------------------+

| 3.141 |

+--------------------+

1 row in set (0.00 sec)

mod 取模

mysql> select mod(10,3);+-----------+

| mod(10,3) |

+-----------+

| 1 |

+-----------+

1 row in set (0.00sec)

mysql> select mod(-10,3);+------------+

| mod(-10,3) |

+------------+

| -1 |

+------------+

1 row in set (0.00 sec)

rand 取随机数

mysql> selectrand();+--------------------+

| rand() |

+--------------------+

| 0.9151140050172005 |

+--------------------+

1 row in set (0.00sec)

mysql> select rand()*10;+-------------------+

| rand()*10 |

+-------------------+

| 8.334071122421019 |

+-------------------+

1 row in set (0.00sec)

mysql> select floor(rand()*10);+------------------+

| floor(rand()*10) |

+------------------+

| 4 |

+------------------+

1 row in set (0.00 sec)

coalesce 转换null值

Null 不会等于或不等于任何值,甚至不能与其自身进行比较,使用如 COALESCE 这样的函数把 Null 转换为一个具体的、可以用于标准评估的值。COALESCE 函数会返回参数列表里的第一个非 Null 值

mysql> create table if not exists t2 (id int(10),comm int(10));

Query OK,0 rows affected, 3 warnings (0.00sec)

mysql> insert into t2 values(1,null),(2,15),(3,18),(4,25),(5,null);

Query OK,5 rows affected (0.01sec)

Records:5 Duplicates: 0 Warnings: 0mysql> select * fromt2;+------+------+

| id | comm |

+------+------+

| 1 | NULL |

| 2 | 15 |

| 3 | 18 |

| 4 | 25 |

| 5 | NULL |

+------+------+

5 rows in set (0.00sec)

mysql> select id,coalesce(comm,0) as a from t2 having a<1;+------+---+

| id | a |

+------+---+

| 1 | 0 |

| 5 | 0 |

+------+---+

2 rows in set (0.00 sec)

MySQL中单行函数concat_MySQL内置函数-单行函数(字符函数)相关推荐

  1. mysql5720_Mysql内置功能《五》 函数

    一 函数 MySQL中提供了许多内置函数,例如: 一.数学函数 ROUND(x,y) 返回参数x的四舍五入的有y位小数的值 RAND() 返回0到1内的随机值,可以通过提供一个参数(种子)使RAND( ...

  2. python中的作用域以及内置函数globals()-全局变量、locals()-局部变量

    在python中,函数会创建一个自己的作用域,也称为为命名空间.这意味着在函数内部访问某个变量时,函数会优先在自己的命名空间中寻找. 通过内置函数globals()返回的是python解释器能知道的变 ...

  3. python中的json函数_python中装饰器、内置函数、json的详解

    装饰器 装饰器本质上是一个Python函数,它可以让其他函数在不需要做任何代码变动的前提下增加额外功能,装饰器的返回值也是一个函数对象. 先看简单例子: def run(): time.sleep(1 ...

  4. Python中匿名函数与内置高阶函数详解

    大家好,从今天起早起Python将持续更新由小甜同学从 初学者的角度 学习Python的笔记,其特点就是全文大多由 新手易理解 的 代码与注释及动态演示 .刚入门的读者千万不要错过! 很多人学习pyt ...

  5. COMSOL中内置数学函数及内置运算符

    前言 最近想通过COMSOL实现一些比较高级的操作,就对内置数学函数和内置运算符相关章节的帮助文档进行了阅读.在COMSOL中使用各类算子及运算符能够在建模时起到很大的帮助,本文将对帮助文档中的相应章 ...

  6. html中写嵌套的js函数,Javascript 嵌套函数 - 递归函数 - 内置函数详解

    在了解了函数的定义和函数调用外,下面我们来介绍一下JavaScript中几种特殊的函数. JavaScript特殊函数有3种: (1)嵌套函数: (2)递归函数: (3)内置函数: 下面详细给大家讲解 ...

  7. decode函数_Python 内置函数总一

    内置函数 python内置函数,截至python3.6.2版本之前一共68个内置函数,内置函数是可以直接使用的函数. Python标准库/内置函数链接:点击此处 内置函数分类 作用域相关 基于字典的形 ...

  8. python内置高阶函数求导_Python——函数式编程、高阶函数和内置函数,及

    Python--函数式编程.高阶函数及内置函数 函数式编程 一.不可变数据:不用变量保存状态不修改变量 二.第一类对象:函数即"变量" 1.函数名可以当做参数传递 2.返回值可以是 ...

  9. JS:函数的内置对象:arguments

    我们可以通过一个案列来理解什么是内置对象,arguments是什么,有什么作用,我们为什么要学 <!DOCTYPE html> <html lang="en"&g ...

最新文章

  1. bugzilla部署
  2. 回复:lerit的关于对象中字段的初始化问题
  3. mysql备份工具Xtrabackup增量备份还原图解
  4. 性能提升2.58倍!阿里最快KV存储引擎揭秘
  5. 订单操作-分页查询所有订单
  6. mysql 分数表实现排名
  7. 书评:JavaFX 2.0:示例介绍
  8. python从爬虫到数据分析项目_零基础学习Python web开发、Python爬虫、Python数据分析,从基础到项目实战!...
  9. cesium坡度坡向分析_综合分析地理空间,科学规划乡村区域
  10. Python学习-将list列表写入文件并读取方法汇总
  11. ArcMap 导入 wrl_ai文件导入c4d没反应怎么办? c4d导入ai源文件失败的原因分析_Illustrator教程_平面设计...
  12. 在 ASP.NET MVC 中创建自定义 HtmlHelper
  13. 考研高等数学张宇30讲笔记——第十讲 积分等式与积分不等式
  14. linux小红伞安装黑屏,在linux下安装Avria(小红伞)
  15. 索爱S318小蜜蜂扩音器性能如何?
  16. Sklearn-scaler对比
  17. html 整个页面淡入浅出,css3网页的淡入淡出效果
  18. mysql账号认证_浅谈MySQL用户账号认证方式
  19. SpringMVC笔记(4):RESTFul详解
  20. 【Mybatis框架】初识Mybatis

热门文章

  1. 人之间的尊重是相互的_人与人之间真心是互换的尊重是相互的
  2. python 变量赋值是引用和拷贝_Python 中变量赋值传递时的引用和拷贝
  3. pythonrange函数用法_python range()函数详细用法
  4. Java学习笔记10---访问权限修饰符如何控制成员变量、成员方法及类的访问范围...
  5. windosw应用提示内存不足
  6. 微信填写服务器配置 php操作方法
  7. 轻量级数据sqlite的C++调用示例
  8. 编译JDK源代码【转】
  9. sqlserver 2008阻止保存要求重新创建表的更改
  10. 白话 discuz加密解密算法,包你懂