当前位置:网站首页>MySQL grouping query rules
MySQL grouping query rules
2022-04-23 03:17:00 【Xiaodaoxian 97】
List of articles
One 、 problem
Think about if you have such a need
There's a bill (bill_detail), A billing form (open_invoice)
Each user has three fee types , They are water charges 、 Sewage fee 、 Liquidated damages .( They correspond to each other cost_type = 01、02、03)
Now you need to query the total amount of arrears of each user , And whether to issue invoice .( notes : Only water charges can be invoiced , Others don't need )
Bill form (bill_detail)
CREATE TABLE `bill_detail` (
`id` VARCHAR(50) NOT NULL COMMENT 'id',
`cost_type` VARCHAR(2) NOT NULL COMMENT ' Fee type 01 charge for water 、02 Sewage fee 、03 Liquidated damages ',
`amount_money` FLOAT(10,2) NOT NULL COMMENT ' The amount owed ',
`user_name` VARCHAR(20) NOT NULL COMMENT ' user name ',
PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT=' Bill details ';
Billing form (open_invoice)
CREATE TABLE `open_invoice` (
`id` VARCHAR(50) NOT NULL COMMENT 'id',
`bill_detail_id` VARCHAR(50) NOT NULL COMMENT ' Bill details id',
`open_invoice_status` INT(1) NOT NULL COMMENT ' Billing status (1 Invoiced , 0 Not invoiced )',
PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT=' Billing form ';
Method 1
We know group by Is the first data after grouping , So we just need to put bill_detail Sort table , So each group ( User name grouping ) data , The expense type is 01 That's the first one
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
Method 2
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
grouping (group by) after , Will select the first data , Join query will change the original query order .(ON The following fields with values are listed in the front ,null rearwards )
Because our topic says , Only the water charge is 01 Will be billed , So after connection query 01 This data must be the first , No need to sort first .
Two 、 The conclusion proves that
There's a list of users (user), And a girlfriend list (girl_friend)
We need to find out everyone's id、 user name 、 Girlfriend name
notes : We know that not everyone has a girlfriend
User table (user)
CREATE TABLE `user` (
`id` varchar(50) NOT NULL COMMENT 'id',
`user_name` varchar(20) NOT NULL COMMENT ' user name ',
PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT=' user ';
Girlfriend watch (girl_friend)
CREATE TABLE `girl_friend` (
`id` varchar(50) NOT NULL COMMENT 'id',
`user_id` varchar(20) NOT NULL COMMENT ' Users ',
`girl_name` varchar(20) NOT NULL COMMENT ' Girlfriend name ',
PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT=' Girlfriend watch ';
Inquire about sql
SELECT
user.id,
user.user_name,
girl.girl_name
FROM user
LEFT JOIN girl_friend girl ON user.id = girl.user_id
Experiment 1
When our table data is as follows :
id | user_name |
---|---|
1 | Zhang San |
2 | Li Si |
3 | Wang Wu |
id | user_id | girl_name |
---|---|---|
1 | 1 | lotus |
The query results are as follows :
id | user_name | girl_name |
---|---|---|
1 | Zhang San | lotus |
2 | Li Si | |
3 | Wang Wu |
Experiment two
The user table does not change , The data of girlfriend table is as follows
id | user_id | girl_name |
---|---|---|
1 | 2 | Big Joe |
1 | 2 | Little Joe |
The query results are as follows :
id | user_name | girl_name |
---|---|---|
2 | Li Si | Big Joe |
3 | Wang Wu | Little Joe |
1 | Zhang San |
版权声明
本文为[Xiaodaoxian 97]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230315096669.html
边栏推荐
- 数据库表中不建索引,在插入数据时,通过sql语句防止重复添加(转载)
- How does Microsoft solve the problem of multiple PC programs
- C WPF UI framework mahapps switching theme
- Flink实时数仓项目—DWS层设计与实现
- poi根据数据创建导出excel
- Data mining series (3)_ Data mining plug-in for Excel_ Estimation analysis
- 【VS Code】解决jupyter文件在vs code中显示异常的问题
- Using stack to solve the problem of "mini parser"
- svg标签中利用<polygon/>循环数组绘制多边形
- EasyUI's combobox implements three-level query
猜你喜欢
[vs Code] solve the problem that the jupyter file displays exceptions in vs code
OLED multi-level menu record
2022G2电站锅炉司炉考试题库及在线模拟考试
2022A特种设备相关管理(电梯)上岗证题库及模拟考试
软件测试相关知识~
Knowledge of software testing~
C read / write binary file
Quartz. Www. 18fu Used in net core
为什么BI对企业这么重要?
Comprehensive calculation of employee information
随机推荐
Xutils3 corrected a bug I reported. Happy
Xamarin effect Chapter 21 expandable floating operation button in GIS
2022g2 boiler stoker examination question bank and online simulation examination
Course design of Database Principle -- material distribution management system
可以接收多种数据类型参数——可变参数
ASP. Net and ASP NETCORE multi environment configuration comparison
Quartz. Www. 18fu Used in net core
Xamarin effect Chapter 22 recording effect
关于idea调试模式下启动特别慢的优化
Mysql database, inconsistent index character set, slow SQL query, interface timeout
Top ten project management software similar to JIRA
Aspnetcore configuration multi environment log4net configuration file
Charles uses three ways to modify requests and responses
編碼電機PID調試(速度環|比特置環|跟隨)
编码电机PID调试(速度环|位置环|跟随)
Flink实时数仓项目—DWS层设计与实现
【VS Code】解决jupyter文件在vs code中显示异常的问题
OLED多级菜单记录
Top 9 task management system in 2022
It can receive multiple data type parameters - variable parameters