当前位置:网站首页>MySql分组查询规则
MySql分组查询规则
2022-04-23 03:15:00 【小道仙97】
一、问题
思考一下假如你有一个这样的需求
有一张账单表(bill_detail),一张开票表(open_invoice)
每一个用户有三种费用类型,分别是水费、污水费、违约金。(分别对应cost_type = 01、02、03)
现在要查询每个用户的欠费总额,和是否开票。(注:只有水费可以开票,其它的不需要)
账单表(bill_detail)
CREATE TABLE `bill_detail` (
`id` VARCHAR(50) NOT NULL COMMENT 'id',
`cost_type` VARCHAR(2) NOT NULL COMMENT '费用类型 01 水费、02 污水费、03 违约金',
`amount_money` FLOAT(10,2) NOT NULL COMMENT '欠费金额',
`user_name` VARCHAR(20) NOT NULL COMMENT '用户名',
PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='账单明细表';
开票表(open_invoice)
CREATE TABLE `open_invoice` (
`id` VARCHAR(50) NOT NULL COMMENT 'id',
`bill_detail_id` VARCHAR(50) NOT NULL COMMENT '账单明细id',
`open_invoice_status` INT(1) NOT NULL COMMENT '开票状态(1 已开票, 0 未开票)',
PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='开票表';
方法一
我们知道group by是取分组后的第一条数据,所以我们只需要把bill_detail表进行排序,这样每一组(用户名分组)数据,费用类型为 01就是第一个了
SELECT
sum(amount_money) money,
invoice.open_invoice_status
FROM
(SELECT * FROM bill_detail ORDER BY cost_type ASC) bill
LEFT JOIN open_invoice invoice ON bill.id = invoice.bill_detail_id
GROUP BY bill.user_name
方法二
SELECT
sum(amount_money) money,
invoice.open_invoice_status
FROM
bill_detail bill
LEFT JOIN open_invoice invoice ON bill.id = invoice.bill_detail_id
GROUP BY bill.user_name
分组(group by)后,会选择第一条数据,连接查询会改变原本的查询顺序。(ON后面的字段有值的排在前面,null在后面)
因为我们题目说了,只有水费为01的才会开票,所以连接查询后01这条数据一定会是第一个,无需先排序。
二、结论证明
有一张用户表(user),和一张女朋友表(girl_friend)
我们要查询出来每个人的id、用户名、女朋友名称
注:我们知道不是每一个人都有女朋友的
用户表(user)
CREATE TABLE `user` (
`id` varchar(50) NOT NULL COMMENT 'id',
`user_name` varchar(20) NOT NULL COMMENT '用户名',
PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户';
女朋友表(girl_friend)
CREATE TABLE `girl_friend` (
`id` varchar(50) NOT NULL COMMENT 'id',
`user_id` varchar(20) NOT NULL COMMENT '所属用户',
`girl_name` varchar(20) NOT NULL COMMENT '女朋友名',
PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='女朋友表';
查询sql
SELECT
user.id,
user.user_name,
girl.girl_name
FROM user
LEFT JOIN girl_friend girl ON user.id = girl.user_id
实验一
当我们的表数据如下:
| id | user_name |
|---|---|
| 1 | 张三 |
| 2 | 李四 |
| 3 | 王五 |
| id | user_id | girl_name |
|---|---|---|
| 1 | 1 | 芙蓉 |
查询结果如下:
| id | user_name | girl_name |
|---|---|---|
| 1 | 张三 | 芙蓉 |
| 2 | 李四 | |
| 3 | 王五 |
实验二
用户表不变,女朋友表数据如下
| id | user_id | girl_name |
|---|---|---|
| 1 | 2 | 大乔 |
| 1 | 2 | 小乔 |
查询结果如下:
| id | user_name | girl_name |
|---|---|---|
| 2 | 李四 | 大乔 |
| 3 | 王五 | 小乔 |
| 1 | 张三 |
版权声明
本文为[小道仙97]所创,转载请带上原文链接,感谢
https://blog.csdn.net/Tomwildboar/article/details/115310509
边栏推荐
- Laravel new route file
- Flink real-time data warehouse project - Design and implementation of DWS layer
- C syntax sugar empty merge operator [?] And null merge assignment operator [? =]
- 全网最全,接口自动化测试怎么做的?精通接口自动化测试详解
- 数据库表中不建索引,在插入数据时,通过sql语句防止重复添加(转载)
- [authentication / authorization] customize an authentication handler
- Tencent video price rise: earn more than 7.4 billion a year! Pay attention to me to receive Tencent VIP members, and the weekly card is as low as 7 yuan
- PID debugging of coding motor (speed loop | position loop | follow)
- Fight leetcode again (290. Word law)
- Find the number of leaf nodes of binary tree
猜你喜欢

ASP. Net 6 middleware series - execution sequence

12.<tag-链表和常考点综合>-lt.234-回文链表

Blazor University (12) - component lifecycle

Ningde's position in the times is not guaranteed?

2022年度Top9的任务管理系统

2022年P气瓶充装培训试题及模拟考试

研讨会回放视频:如何提升Jenkins能力,使其成为真正的DevOps平台

Charles uses three ways to modify requests and responses

2022 P cylinder filling training test questions and simulation test

2022T电梯修理考试模拟100题及在线模拟考试
随机推荐
Comprehensive calculation of employee information
Xutils3 corrected a bug I reported. Happy
JS recursive tree structure calculates the number of leaf nodes of each node and outputs it
可以接收多種數據類型參數——可變參數
Preview of converting doc and PDF to SWF file
Flink real-time data warehouse project - Design and implementation of DWS layer
C introduction of variable parameter params
[MySQL] left function | right function
Use split to solve the "most common words" problem
Experiment 5 components and event handling
General testing technology [1] classification of testing
C syntax pattern matching [switch expression]
IOTOS物联中台对接海康安防平台(iSecure Center)门禁系统
中后二叉建树
编码电机PID调试(速度环|位置环|跟随)
Xamarin effect Chapter 21 expandable floating operation button in GIS
Mysql database, inconsistent index character set, slow SQL query, interface timeout
一文了解全面静态代码分析
Mysql database design specification
LoadRunner - performance testing tool