欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

mysql/mariadb学习记录——查询2

程序员文章站 2022-06-30 11:55:26
Alias——使用一个列名别名AS 关键字: GROUP BY语句: 比如在合计(sum函数)的时候常需要用group by语句来进行分类求和 2018-05-01 23:21:52 ......

Alias——使用一个列名别名AS 关键字:

mysql> select sno as studentId,sname as studentName from student;
+-----------+-------------+
| studentId | studentName |
+-----------+-------------+
| 12001     | bgg         |
| 96001     | 刘军        |
| 95002     | 刘晨        |
| 94001     | 山寨        |
| 95005     | 张三        |
| 95004     | 张立        |
| 95001     | 李勇        |
| 97001     | 李四        |
| 95003     | 王敏        |
| 96004     | 芙蓉        |
+-----------+-------------+
10 rows in set (0.10 sec)

//在mysql/mariadb中省略as 关键字也可实现
mysql> select sno studentId,sname studentName from student;
+-----------+-------------+
| studentId | studentName |
+-----------+-------------+
| 12001     | bgg         |
| 96001     | 刘军        |
| 95002     | 刘晨        |
| 94001     | 山寨        |
| 95005     | 张三        |
| 95004     | 张立        |
| 95001     | 李勇        |
| 97001     | 李四        |
| 95003     | 王敏        |
| 96004     | 芙蓉        |
+-----------+-------------+
10 rows in set (0.07 sec)

GROUP BY语句:

比如在合计(sum函数)的时候常需要用group by语句来进行分类求和

//emp表格中的数据
mysql> select * from emp;
+-------+--------+-----------+------+------------+---------+---------+--------+
| empno | ename  | job       | mgr  | hiredate   | sal     | comm    | deptno |
+-------+--------+-----------+------+------------+---------+---------+--------+
|  7369 | SMITH  | CLERK     | 7902 | 1980-12-17 |  800.00 |    NULL |     20 |
|  7499 | ALLEN  | SALESMAN  | 7698 | 1981-02-20 | 1600.00 |  300.00 |     30 |
|  7521 | WARD   | SALESMAN  | 7698 | 1981-02-22 | 1250.00 |  500.00 |     30 |
|  7566 | JONES  | MANAGER   | 7839 | 1981-04-02 | 2975.00 |    NULL |     20 |
|  7654 | MARTIN | SALESMAN  | 7698 | 1981-09-28 | 1250.00 | 1400.00 |     30 |
|  7698 | BLAKE  | MANAGER   | 7839 | 1981-05-01 | 2850.00 |    NULL |     30 |
|  7782 | CLARK  | MANAGER   | 7839 | 1981-06-09 | 2450.00 |    NULL |     10 |
|  7788 | SCOTT  | ANALYST   | 7566 | 1987-04-19 | 3000.00 |    NULL |     20 |
|  7839 | KING   | PRESIDENT | NULL | 1981-11-17 | 5000.00 |    NULL |     10 |
|  7844 | TURNER | SALESMAN  | 7698 | 1981-09-08 | 1500.00 |    0.00 |     30 |
|  7876 | ADAMS  | CLERK     | 7788 | 1987-05-23 | 1100.00 |    NULL |     20 |
|  7900 | JAMES  | CLERK     | 7698 | 1981-12-03 |  950.00 |    NULL |     30 |
|  7902 | FORD   | ANALYST   | 7566 | 1981-12-03 | 3000.00 |    NULL |     20 |
|  7934 | MILLER | CLERK     | 7782 | 1982-01-23 | 1300.00 |    NULL |     10 |
+-------+--------+-----------+------+------------+---------+---------+--------+

//查找每个job种类一共支出的sal是多少
mysql> select job,sum(sal) from emp group by job;
+-----------+----------+
| job       | sum(sal) |
+-----------+----------+
| ANALYST   |  6000.00 |
| CLERK     |  4150.00 |
| MANAGER   |  8275.00 |
| PRESIDENT |  5000.00 |
| SALESMAN  |  5600.00 |
+-----------+----------+
5 rows in set (0.12 sec)

//查看student表中的男女生人数
mysql> select ssex,count(sage) from student group by ssex;
+------+-------------+
| ssex | count(sage) |
+------+-------------+
| M    |           1 |
| 女   |           3 |
| 男   |           6 |
+------+-------------+

2018-05-01  23:21:52