Select进阶查询·查询结果处置
发布时间:2023-10-19 12:36:49 所属栏目:语言 来源:
导读:对用select 命令查找到的数据再做处理,
类似于系统命令管道 例如 ps aux | grep httpd
语法格式:
语法格式1:不加条件查找数据
select 字段名列表 from 库.表 分组|排序|过滤|分页 ;
语法格式2:添加条件查
类似于系统命令管道 例如 ps aux | grep httpd
语法格式:
语法格式1:不加条件查找数据
select 字段名列表 from 库.表 分组|排序|过滤|分页 ;
语法格式2:添加条件查
对用select 命令查找到的数据再做处理, 类似于系统命令管道 例如 ps aux | grep httpd 语法格式: 语法格式1:不加条件查找数据 select 字段名列表 from 库.表 分组|排序|过滤|分页 ; 语法格式2:添加条件查找数据 select 字段名列表 from 库.表 where 筛选条件 分组|排序|过滤|分页 ; 关键词: 分类 命令/符号/... 说明/注意 分组 group by 表头名 表头值相同为一组,值只显示一次 注意: 分组命令只能单独使用, 或与聚集函数一起使用 排序 order by 表头名 对表头下的数据进行排序 针对数值类型的表头进行排序 order by 表头名 asc 升序,不写asc为默认升序 order by 表头名 desc 降序 过滤 having 筛选条件 对select查询到的数据 做筛选 分页 limit n1 n1:只显示查询结果的第n1行 limit n1,n2 n1:表示起始行(从0开始计数) n2:表示显示的总行数 四、使用案例: 分组group by: # 查看每个部门的人数 # 按照部门编号分组统计员工名的个数 mysql>select dept_id, count(name) from tarena.employees group by dept_id; +---------+-------------+ | dept_id | count(name) | +---------+-------------+ | 1 | 8 | | 2 | 5 | | 3 | 6 | | 4 | 55 | +---------+-------------+ rows in set (0.00 sec) 排序order by:运算符横着计算,统计函数竖着计算 # 把2018年每个员工的总收入由高到底排序 mysql> select employee_id, sum(basic+bonus) as total -> from tarena.salary where year(date)=2018 group by -> employee_id order by total desc; +-------------+--------+ | employee_id | total | +-------------+--------+ | 117 | 374923 | | 31 | 374923 | | 37 | 362981 | | 68 | 360923 | ... +-------------+--------+ 120 rows in set (0.01 sec) 过滤having: # 查找部门人数少于10人的部门名称及人数 mysql> select dept_id,count(name),from,tarena.employees -> group by dept_id having count(name)<10; +---------+-------------+ | dept_id | count(name) | +---------+-------------+ | NULL | 3 | | 1 | 8 | | 2 | 5 | | 3 | 6 | | 6 | 9 | | 8 | 3 | +---------+-------------+ 6 rows in set (0.00 sec) 分页limit: # 只显示查询结果的前3行 mysql> select * from tarena.user where shell is not null limit 3; +----+--------+----------+------+------+---------+---------+---------------+ | id | name | password | uid | gid | comment | homedir | shell | +----+--------+----------+------+------+---------+---------+---------------+ | 1 | root | x | 0 | 0 | root | /root | /bin/bash | | 2 | bin | x | 1 | 1 | bin | /bin | /sbin/nologin | | 3 | daemon | x | 2 | 2 | daemon | /sbin | /sbin/nologin | +----+--------+----------+------+------+---------+---------+---------------+ 3 rows in set (0.01 sec) # 只显示查询结果的第1行 到 第3 # 0:表示查询结果的第1行 # 3:表示查询的行数 mysql> select * from user where shell is not null limit 0,3; +----+--------+----------+------+------+---------+---------+---------------+ | id | name | password | uid | gid | comment | homedir | shell | +----+--------+----------+------+------+---------+---------+---------------+ | 1 | root | x | 0 | 0 | root | /root | /bin/bash | | 2 | bin | x | 1 | 1 | bin | /bin | /sbin/nologin | | 3 | daemon | x | 2 | 2 | daemon | /sbin | /sbin/nologin | +----+--------+----------+------+------+---------+---------+---------------+ 3 rows in set (0.00 sec) (编辑:马鞍山站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐