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