mysql/mariadb学习记录——查询2
Alias——使用一个列名别名AS 关键字:
mysql> select sno as studentId,sname as studentName from student;
+-----------+-------------+
| studentId | studentName |
+-----------+-------------+
| | bgg |
| | 刘军 |
| | 刘晨 |
| | 山寨 |
| | 张三 |
| | 张立 |
| | 李勇 |
| | 李四 |
| | 王敏 |
| | 芙蓉 |
+-----------+-------------+
rows in set (0.10 sec) //在mysql/mariadb中省略as 关键字也可实现
mysql> select sno studentId,sname studentName from student;
+-----------+-------------+
| studentId | studentName |
+-----------+-------------+
| | bgg |
| | 刘军 |
| | 刘晨 |
| | 山寨 |
| | 张三 |
| | 张立 |
| | 李勇 |
| | 李四 |
| | 王敏 |
| | 芙蓉 |
+-----------+-------------+
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的更多相关文章
- mysql/mariadb学习记录——查询
连接查询:同时设计两个及以上的表的查询 连接条件或连接谓词:用来连接两个表的条件一般格式: [<表名1>]<列名1> <比较运算符> [<表名2>]&l ...
- mysql/mariadb学习记录——查询3(AVG、SUM、COUNT)函数
AVG() 求平均数函数: //求emp表中的sal属性的平均值 mysql> select avg(sal) as salAverage from emp; +-------------+ | ...
- mysql/mariadb学习记录——连接查询(JOIN)
//本文使用的数据表格//persons表中id_p为主键//orders表中id_o为主键,id_p为外键参考persons表中的id_p mysql> select * from perso ...
- mysql/mariadb学习记录——limit
在mysql/mariadb 中可以用limit来限制查询的条数.例子如下: 1.limit后加一个参数 limit n: //选中查询所有结果中的前两条记录并返回 mysql> ; +---- ...
- mysql/mariadb学习记录——创建删除数据库、表的基本命令
查看已有的数据库: mysql> show databases; +--------------------+ | Database | +--------------------+ | inf ...
- MySQL/MariaDB数据库的查询缓存优化
MySQL/MariaDB数据库的查询缓存优化 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.MySQL架构 Connectors(MySQL对外提供的交互接口,API): ...
- MySQL/MariaDB学习笔记——mysql.user表中存在多个root用户问题理解
mysql.user表中存在多个root用户问题 问题描述:使用 SELECT host,user FROM mysql.user 发现mysql.user表中存在三个root用户,如下 持着对中几个 ...
- MariaDB学习记录
MariaDB的学习 MariaDB的学习 关于MariaDB的历史,不再概述 下面是mariadb的官网:https://mariadb.com/ 同样的,MariaDB一样有连接java的jar包 ...
- mysql的学习记录
1 MySQL -h localhost -u UserName -p Password-h不写,默认为localhost注意:最好先MySQL -h localhost -u UserName -p ...
随机推荐
- linux 挂载命令mount、umount
mount /bin/mount语法:mount [-t文件系统] 设备文件名 挂载点mount -t iso9660 /dev/sr0 /mnt/cdromiso9660是固定的,光盘:所以 -t ...
- 【Java】生成UUID
import java.util.UUID; public class MainProcess { public static void main(String[] args) { UUID uuid ...
- 混淆矩阵(Confusion matrix)的原理及使用(scikit-learn 和 tensorflow)
原理 在机器学习中, 混淆矩阵是一个误差矩阵, 常用来可视化地评估监督学习算法的性能. 混淆矩阵大小为 (n_classes, n_classes) 的方阵, 其中 n_classes 表示类的数量. ...
- PHP匹配当前传入是何种类型
本文出至:新太潮流网络博客 /** * [is_string_regular_type 正则自动验证传入数据] * @E-mial wuliqiang_aa@163.com * @TIME 2017- ...
- Debian 版本历史
Production Releases Version Code name Release date End of life date 8.0 jessie April 25th 2015 7.0 ...
- nodejs+redis使用
安装 linux安装及配置之前写过了http://www.cnblogs.com/zycbloger/p/6226682.html windows安装 下载地址:https://github.com/ ...
- 一些简单的SQL Server服务器监控
1:磁盘活动的一些监控 指标 吞吐量:IOPS,存储子系统每秒能提供多少次I/O 吞吐量,MB/S,I/O子系统每秒能提供多少MB 延时:每个I/O请求占用多少时间 队列长度:队列中有多少IO请求在等 ...
- Script" References MACLEAN‘s post Speed up the index creation.
alter session set workarea_size_policy=MANUAL; alter session set db_file_multiblock_read_count=512; ...
- Linux系统锁定关键文件
锁定系统关键文件 1.密码.账号文件 chattr +i /etc/passwd /etc/group /etc/shadow /etc/gshadow /etc/inittab 加锁:chattr ...
- 《C++ Primer Plus》读书笔记之十二—C++中的代码重用
第14章 C++中的代码重用 1.C++代码重用方法:公有继承.使用本身是另一个类的对象的类成员(这种方法称为包含.组合或层次化).私有或保护继承.类模板等. 2.模板特性意味着声明对象时,必须指定具 ...