码迷,mamicode.com
首页 > 数据库 > 详细

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

时间:2018-05-02 02:29:17      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:clerk   art   code   sha   sql   学习记录   记录   分类   resid   

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

  

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

标签:clerk   art   code   sha   sql   学习记录   记录   分类   resid   

原文地址:https://www.cnblogs.com/joy9707/p/8978043.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!