MySQL数据库

Select进阶查询·数字计算

2023-01-23  本文已影响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 员工编号

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)
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)
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)
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)
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)
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)
上一篇下一篇

猜你喜欢

热点阅读