Mysql常用运算符与函数汇总

本文给大家汇总介绍了mysql中的常用的运算符以及常用函数的用法及示例,非常的全面,有需要的小伙伴可以参考下

我们先把数据表建好

use test;
create table `employee`(
emp_no int unsigned,
emp_name varchar(30),
emp_sex varchar(3),
emp_age tinyint unsigned,
sal double,
history datetime
);
insert into employee values(1, '张三', '男', 18, 5000, '2012-04-23'),
(2, '李四', '男', 27, 4500, '2013-05-23'),
(3, '王五', '男', 23, 4700, '2012-04-21'),
(4, '子龙', '男', 19, 3800, '2011-03-04'),
(5, '李白', '男', 15, 6200, '2015-09-09'),
(6, '刘备', '男', 28, 2500, '2016-02-11'),
(7, '吕布', '男', 21, 6000, '2010-10-18'),
(8, '尚香', '女', 16, 4500, '2011-09-26'),
(9, '小乔', '女', 15, null, '2013-07-05'),
(10, '大乔', '女', 16, 5000, '2017-09-01');

常用的运算符:
1: 等于( = )

select * from employee where sal = 3800;
select * from employee where sal = null; --这里查询不到为null的数据

2: 等于( <=> )

select * from employee where sal <=> 3800;
select * from employee where sal <=> null; --这里可以查询到为null的数据
 
3: is判断(null)
select * from employee where sal is null;
select * from employee where sal is not null;
 
4: null值判断还可以使用isnull();

select * from employee where isnull(sal);
select * from employee where !isnull(sal);

5: 在区间(beween)内  between min and max  ps:这里是一个闭区间

select * from employee where sal between 4500 and 5000;

6: 不在区间内

select * from employee where sal not between 4500 and 5000;  --null不为包括进去

7: and 和 or

select * from employee where sal not between 4500 and 5000 or sal is null;
select * from employee where sal = 4500 and emp_sex = '女';

8: 小于(<), 大于(>), 小于等于(<=), 大于等于(>=)

select * from employee where sal >= 4500;

*******************************************************************************************

数学函数
1: rand();

select rand() from dual; --dual是一个伪表
select 1+1 from dual;
select rand(); --可以简写
2: least(value1, value2, ...) 返回最小值

select least(54,76,4,65,76,87,87,56,65,654,45,23,1,76);
select least(54,76,4,65,76,87,87,56,65,654,45,23,1,76) as min_value; --列名可以起一个别名
3: greatest(value1, value2, ...) 返回最大值
select greatest(54,76,4,65,76,87,87,56,65,654,45,23,1,76);

4: round(M, D); 返回M的四舍五入的值, D表示要保留几们小数,默认值是0
select round(1.69);
select round(1.69, 1);
5: abs() 绝对值
select 5-10;
select abs(5-10);
***************************************************************************************************************

汇总函数

1: avg();
select * from employee where sal >= 6000;
select avg(sal) from employee where sal >= 6000;
2: count()
select count(*) from employee;
select count(emp_name) from employee;
select count(sal) from employee; --打印9 这里会忽略null值
select count(*) from employee where sal >= 4000;
select count(*) from employee where sal <= 4000 or sal is null;
3: sum()
select sum(sal) from employee where sal >= 6000;

4: min()
select min(sal) from employee;

5: max()
select max(sal) from employee;
***************************************************************************************************************

日期函数
1: 获取当前的日期时间
select now(), sysdate(), current_timestamp();
select now(6), sysdate(6), current_timestamp(6);
ps: now(), current_timestamp();没有区别, 表示sql开始执行时的时间
sysdate()表示这个函数开始时间
2: 获取当前日期
select curdate(); --只有年月日

3: 获取当前时间
select curtime(); --只有时分秒

4: 日期的加运算date_add
select history, date_add(history, interval '1 12:10' day_minute) from employee; --date_add(history, interval '1 12:10' day_minute)
select history, date_add(history, interval '1-1' year_month) from employee; --date_add(history, interval '1-1' year_month)
select history, date_add(history, interval '1' second) from employee; --date_add(history, interval '1' second)
5: 日期的减运算data_sub
select history, date_sub(history, interval '1-1' year_month) from employee;

6: 计算日期差
select history, sysdate(), datediff(sysdate(), history) from employee; --以天数来表示

7: 获取日期的指定部分(把日期转换为指定的格式) date_format()
select history, date_format(history, '%Y年%m月%d号') from employee;
select history, date_format(history, '%d号') from employee;
select history, date_format(history, '%Y年%m月%d号 %H时%i分%s秒') from employee;
8: 计算出一个日期是星期几
select history, dayname(history) from employee;

9: 中文日期字符串转换日期str_to_date()
insert into employee values(11, '张飞', '男', 22, 3000, '2017年02月01号'); --报错
insert into employee values(11, '张飞', '男', 22, 3000, str_to_date('2017年02月01号', '%Y年%m月%d号 %H时%i分%s秒'));
insert into employee values(12, '二哥', '男', 22, 3000, str_to_date('2017年02月01号 23时02分02秒', '%Y年%m月%d号 %H时%i分%s秒'));
insert into employee values(12, '二哥', '男', 22, 3000, str_to_date('2017年02月01号 11时02分02秒', '%Y年%m月%d号 %h时%i分%s秒'));
ps: 如果是h则表示12小制, 如果是大H则表示24小明制;

字符串函数
1: left(str, len) 返回字符串str的左端len个字符
select left('abcdefg', 5);

2: length()
select length('abcdefg');

3: lower(str) 返回小写的字符串str
select lower('HELLO');

4: substring() 取子字符串, 第二个参数是截取的起始位置, 第三个参数是要截取的长度
select substring('helloworld',2,3);

5: concat() 字符串拼接
select concat(emp_name, '员工') from employee;

6: replace(替换
select replace(emp_name, '李', '老') from employee where emp_name = '李四';

 

Mysql常用运算符与函数汇总的更多相关文章

  1. MYSQL常用内置函数详解说明

    函数中可以将字段名当作变量来用,变量的值就是该列对应的所有值:在整理98在线字典数据时(http://zidian.98zw.com/),有这要一个需求,想从多音字duoyinzi字段值提取第一个拼音 ...

  2. MySQL常用的系统函数

    MySQL常用的系统函数 2019年01月17日 17:49:14 pan_junbiao 阅读数 155    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csd ...

  3. MYSQL常见运算符和函数

    字符函数 (1)CONCAT():字符连接 SELECT CONCAT('IMOOC','-','MySQL');//IMOOC-MySQL SELECT CONCAT (first_name,las ...

  4. MYSQL常见运算符和函数【重要】

    字符函数 (1)CONCAT():字符连接 SELECT CONCAT(‘IMOOC’,’-‘,’MySQL’);//IMOOC-MySQL SELECT CONCAT (first_name,las ...

  5. MySQL日期和时间函数汇总

    本文基于MySQL8.0 本文介绍MySQL关于日期和时间操作的函数. 日期和时间函数 函数 描述 ADDDATE() 给日期值添加时间值 ADDTIME() 添加time CONVERT_TZ() ...

  6. MySQL 常用的聚合函数

    [常用的聚合函数] mysql聚合函数一般用户统计一列值进行计算,然后返回计算结果.一般于分组group by 配合使用. count //统计个数 select count(*) from test ...

  7. MySQL(六) —— 运算符和函数

    1. 字符函数   函数名称 描述 CONCAT()         字符连接                             CONCAT_WS()          使用指定的分隔符进行字 ...

  8. MySQL常用的数学函数

    在使用mysql自带的函数要慎重,说是会影响数据执行效率,代价太大.这个也要区分开,区分快软件的引用范畴,比如说内部系统业务逻辑比较复杂,功能点很细,但是并发量不是很大,这个时候用MySQL自带的函数 ...

  9. MySQL报错注入函数汇总及常用注入语句

    版权声明:本文转载自网络内容,下面附原创链接原创链接:https://blog.csdn.net/Auuuuuuuu/article/details/91415165 常用函数 字符串连接函数,将多个 ...

随机推荐

  1. 51nod1108(曼哈顿距离)

    题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1108 题意:中文题诶- 思路:曼哈顿距离,题目没要求所求点要在 ...

  2. uoj#388. 【UNR #3】配对树(线段树合并)

    传送门 先考虑一个贪心,对于一条边来说,如果当前这个序列中在它的子树中的元素个数为奇数个,那么这条边就会被一组匹配经过,否则就不会 考虑反证法,如果在这条边两边的元素个数都是偶数,那么至少有两组匹配经 ...

  3. 洛谷P3757 [CQOI2017]老C的键盘

    传送门 首先可以直接把整个序列建成一个完全二叉树的结构,这个应该都看得出来 然后考虑树形dp,以大于为例 设$f[i][j]$表示$i$这个节点在子树中排名第$j$位时的总方案数(因为实际只与相对大小 ...

  4. pytest框架(一)

    代码示例一 # coding=utf-8 def func(x): return x + 1 def test_answer(): assert func(3) == 5 运行结果 E:\pyYouY ...

  5. web项目部署后动态编译无法找到依赖的jar包

    很纳闷的一个问题,通过配置文件生成的java源码在本地动态编译没有问题,但是部署服务器后编译不通过,找不到依赖的jar包. 通过网上查资料,找到一个兄弟提供的方法,问题解决了:下面贴出代码以供参考: ...

  6. 小技巧(updating)

    小技巧 我们要算一个点集中所有点到另一个点集中所有点的一些量的时候,可以建立一个超级源点和超级汇点,从多->多变成单->单 整体二分的时候,操作要可以撤销,才能保证复杂度,每一层到左边区间 ...

  7. Uva11572

    读入可以不需要存入数组 #include<bits/stdc++.h> #define inf 0x3f3f3f3f //const int maxn=; using namespace ...

  8. (转)C#抽象类和接口对比

    c#中抽象类(abstract)和接口(interface)的相同点与区别  转自:http://blog.csdn.net/fxh_hua/archive/2009/08/20/4464739.as ...

  9. 走进docker的世界之入门篇

    by zhouzhipeng from https://blog.zhouzhipeng.com/walk-in-docker-beginning.html本文可全文转载,但需要保留原作者和出处. 什 ...

  10. 现阶段github上的emysql编译无法通过的问题

    最近在写db引擎,今天用到了emysql,找到https://github.com/Eonblast/Emysql,拽下来,然后发现竟然编译不通过~~去网上找了下资料,在 http://erlang. ...