美文网首页MySQL数据库
Select进阶查询·数字计算

Select进阶查询·数字计算

作者: 技术老男孩 | 来源:发表于2023-01-23 00:07 被阅读0次

一、定义:

  • 符号两边 一定要是数字 或 数值类型的表头
  • 计算符号:加(+)、减(-)、乘(*)、除(/)、取余(%)
  • 运算符是横向计算(函数是垂直运算)

二、使用案例:

  • 加法
# 输出8号员工2019年1月10 工资总和
mysql> select employee_id ,date , basic +  bonus  as 总工资 from tarena.salary 
where employee_id = 8 and date=20190110;
+-------------+------------+----------------+
| employee_id | date       |     总工资       |
+-------------+------------+----------------+
|           8 | 2019-01-10 |          24093 |
+-------------+------------+----------------+
  • 乘法
# 查看8号员工2019年1月10 基本工资翻3倍的 值
mysql> select employee_id , basic , basic * 3  as 工资翻三倍  from tarena.salary 
where  employee_id=8  and date=20190110;
+-------------+-------+-----------------+
| employee_id | basic | 工资翻三倍      |
+-------------+-------+-----------------+
|           8 | 23093 |           69279 |
+-------------+-------+-----------------+
1 row in set (0.00 sec)

三、综合应用:

salary 工资表:

employee_id 员工编号
basic 基本工资
bonus 奖金
date 发工资日期

employees 员工表

birth_date 生日
employee_id 员工编号

  • 查看1号、3号、5号 员工 2020年1月10号 的基本工资和奖金
mysql> select employee_id ,basic,bonus from tarena.salary 
where employee_id in (1,3,5) and  date=20200110;
+-------------+-------+-------+
| employee_id | basic | bonus |
+-------------+-------+-------+
|           1 | 17866 |  3000 |
|           3 | 10210 |  4000 |
|           5 | 17866 |  1000 |
+-------------+-------+-------+
3 rows in set (0.00 sec)
  • 分别计算 1号、3号、5号 员工 2020年1月10号 的工资总和
mysql> select employee_id ,basic+bonus from tarena.salary where employee_id in (1,3 ,5) and  date=20200110;
+-------------+-------------+
| employee_id | basic+bonus |
+-------------+-------------+
|           1 |       20866 |
|           3 |       14210 |
|           5 |       18866 |
+-------------+-------------+
3 rows in set (0.00 sec)
  • 计算 1号、3号、5号 三个员工 2020年1月10号 总工资相加的和
mysql> select sum(basic+bonus) from tarena.salary where employee_id in (1,3 ,5) and  date=20200110;
+------------------+
| sum(basic+bonus) |
+------------------+
|            53942 |
+------------------+
1 row in set (0.00 sec)
  • 查看8号员工的出生年份
mysql> select employee_id , year(birth_date) as 出生年份 from tarena.employees
 where employee_id = 8 ; 
+-------------+--------------+
| employee_id | 出生年份     |
+-------------+--------------+
|           8 |         1993 |
+-------------+--------------+
1 row in set (0.00 sec)
  • 查看8号员工的出生月份
mysql> select employee_id , month(birth_date) as 出生月份 from tarena.employees 
where employee_id = 8 ;
+-------------+--------------+
| employee_id | 出生月份     |
+-------------+--------------+
|           8 |            3 |
+-------------+--------------+
1 row in set (0.00 sec)
  • 查看8号员工的出生年份和年龄
mysql> select employee_id ,year(birth_date) as 出生年份 , 2022 - year(birth_date) as 年龄 from tarena.employees where employee_id = 8 ;
+-------------+--------------+------+
| employee_id | 出生年份     | 年龄  |
+-------------+--------------+------+
|           8 |         1993  |   29 |
+-------------+--------------+------+
1 row in set (0.00 sec)

相关文章

  • Select进阶查询·数字计算

    一、定义: 符号两边 一定要是数字 或 数值类型的表头 计算符号:加(+)、减(-)、乘(*)、除(/)、取余(%...

  • sqlP39-P48

    #进阶3:排序查询 /* #引入 SELECT * FROM employees 语法:执行顺序 select 查...

  • mysql排序查询

    进阶3:排序查询 /*语法:select 查询列表from 表名【where 筛选条件】order by 排序的...

  • mysql分组查询

    进阶5:分组查询 /*语法: select 查询列表from 表【where 筛选条件】group by 分组的字...

  • 查询DQL

    进阶1:基础查询 /*语法:select 查询列表 from 表名; 类似于:System.out.println...

  • mysql基础查询

    进阶1:基础查询 /*语法:select 查询列表 from 表名; 类似于:System.out.println...

  • sql P1-P38

    #进阶1:基础查询 /* 语法: select 查询列表 from 表名; 类似于: System.out.pri...

  • Select进阶查询·连接查询

    一、连接查询定义: 把多张表通过连接条件临时组成1张新表,在临时的新表里有连接表的所有表头和数据。 连接查询分类:...

  • Select进阶查询·子查询

    一、子查询定义: select查询命令里包含select查询命令,包涵的select 命令 放在() 里 包含的s...

  • SQL第3/n篇(持续更新中)子查询很重要

    进阶7 子查询 含义:子查询(内查询):出现在其他语句中的select语句主查询(外查询):外部的查询语句分类...

网友评论

    本文标题:Select进阶查询·数字计算

    本文链接:https://www.haomeiwen.com/subject/makucdtx.html