当前位置:网站首页>Highly qualified SQL writing: compare lines. Don't ask why. Asking is highly qualified..
Highly qualified SQL writing: compare lines. Don't ask why. Asking is highly qualified..
2022-07-02 20:23:00 【Java technology stack】
Environmental preparation
Database version : MySQL 5.7.20-log

Build table SQL:
DROP TABLE IF EXISTS `t_ware_sale_statistics`;CREATE TABLE `t_ware_sale_statistics` ( `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT ' Primary key id', `business_id` bigint(20) NOT NULL COMMENT ' Business organization code ', `ware_inside_code` bigint(20) NOT NULL COMMENT ' Commodity self coding ', `weight_sale_cnt_day` double(16,4) DEFAULT NULL COMMENT ' Average daily sales ', `last_thirty_days_sales` double(16,4) DEFAULT NULL COMMENT ' lately 30 Daily sales ', `last_sixty_days_sales` double(16,4) DEFAULT NULL COMMENT ' lately 60 Daily sales ', `last_ninety_days_sales` double(16,4) DEFAULT NULL COMMENT ' lately 90 Daily sales ', `same_period_sale_qty_thirty` double(16,4) DEFAULT NULL COMMENT ' In the same period last year 30 Daily sales ', `same_period_sale_qty_sixty` double(16,4) DEFAULT NULL COMMENT ' In the same period last year 60 Daily sales ', `same_period_sale_qty_ninety` double(16,4) DEFAULT NULL COMMENT ' In the same period last year 90 Daily sales ', `create_user` bigint(20) DEFAULT NULL COMMENT ' founder ', `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT ' Creation time ', `modify_user` bigint(20) DEFAULT NULL COMMENT ' Final modifier ', `modify_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT ' Final revision time ', `is_delete` tinyint(2) DEFAULT '2' COMMENT ' Whether or not to delete ,1: yes ,2: no ', PRIMARY KEY (`id`) USING BTREE, KEY `idx_business_ware` (`business_id`,`ware_inside_code`) USING BTREE) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC COMMENT=' Commodity sales statistics ';
- 1.

Initialization data
To prepare the 769063 Data

Demand background
Sales of goods under the business organization , The same business organization can sell different goods , The same commodity can be sold in different business institutions , That is to say : The relationship between business organization and commodity is many to many
Suppose there are now n Organizations , There are several commodities under each organization , How to query the sales of goods in these stores ?
Be specific , It's like this

How to find out 100001 Lower commodity 1000、1001、1003 、 100002 Lower commodity 1003、1004 、 100003 Lower commodity 1006、1008、1009 Sales of
It's like a double-layer list ( A set of goods list in the business organization list ) Query for ; Neither the business organization list nor the commodity list is fixed , It's dynamic
So the problem is : How to query multiple business organizations , Sales of some goods ( Once I described the problem , Maybe it's more blurred , If only everyone understood the meaning !)
Circular query
It's easy to think of , Circulate the list of business institutions at the code level , Each business organization checks the database once , The pseudocode is as follows :

Concrete SQL It's like this

SQL Can go index

Implement a simple , Also understand ,SQL You can also go , Everything seems perfect
But the reality is : Department development specification constraints , You can't cycle through the database
Oh, Ho , This way can only give up , Find another way
OR Splicing
adopt MyBatis Of dynamic SQL function , Conduct SQL Splicing , It's like this

Concrete SQL It's like this

SQL You can also go

Implement a simple , Also understand ,SQL You can also go , And only query the database once , It seems feasible
The only pity is : It's a little expensive OR, If there are many business institutions , that SQL It will be longer
As one of the candidates , Let's move on
Mixed query and filtering
It's also using Mybatis Of dynamic SQL , take business_id Put the lists together 、 ware_inside_code Put together , It's like this

Concrete SQL It's like this

SQL You can also go

Implement a simple , Also understand ,SQL You can also go , And only query the database once , It seems to work
however : The result set found is greater than or equal to the result set we want , Your products , Your delicacies !
Therefore, you also need to filter the found result set , Filter out the result set we want
Let's be one of the candidates , Let's move on
Line by line comparison
SQL-92 Line to line comparison function is added in , thus , Comparison predicate = 、< 、> and IN The parameters of the predicate are no longer just scalar values , It can also be a list of values
Of course , Still have to use Mybatis Of dynamic SQL , It's like this

Concrete SQL It's like this

SQL Can also walk

Implement a simple ,SQL You can also go , And only query the database once , Feel feasible
It's just : It's a little hard to understand , Because we usually use so little , So this kind of writing looks strange
in addition , The line comparison is SQL standard , Not a specification for a relational database , In other words, relational databases should support this writing method
summary
1、 Finally, I chose Line by line comparison This way to achieve the requirements , Don't ask me why , Asking is forcing gegao !
2、 There are many ways to realize a requirement , We need to consider the business and various constraints , Choose the most suitable one
3、 The line comparison is SQL-92 Introduced in ,SQL-92 yes 1992 Specifications formulated in , Line to line comparison is not a new feature , It's a basic function that has existed for a long time !
I think it's good , Don't forget to like it + Forward !
边栏推荐
- 【Hot100】23. 合并K个升序链表
- 【Hot100】21. 合并两个有序链表
- Want to ask, is there any discount for opening an account now? Is it safe to open an account online?
- AcWing 340. Solution to communication line problem (binary + double ended queue BFS for the shortest circuit)
- 【实习】解决请求参数过长问题
- KT148A语音芯片使用说明、硬件、以及协议、以及常见问题,和参考代码
- 【Hot100】21. Merge two ordered linked lists
- AcWing 181. Turnaround game solution (search ida* search)
- Codeforces round 651 (Div. 2) (a thinking, B thinking, C game, D dichotomy, e thinking)
- Common problems and description of kt148a voice chip IC development
猜你喜欢

Common problems and description of kt148a voice chip IC development

Py's interpret: a detailed introduction to interpret, installation, and case application

KT148A语音芯片使用说明、硬件、以及协议、以及常见问题,和参考代码

Exemple complet d'enregistrement du modèle pytoch + enregistrement du modèle pytoch seuls les paramètres d'entraînement sont - ils enregistrés? Oui (+ Solution)

SBT tutorial

After eight years of test experience and interview with 28K company, hematemesis sorted out high-frequency interview questions and answers

Automatically generate VGg image annotation file

Driverless learning (4): Bayesian filtering

API documentation tool knife4j usage details

Kt148a voice chip IC user end self replacement voice method, upper computer
随机推荐
【871. 最低加油次数】
Second hand housing data analysis and prediction system
Sometimes only one line of statements are queried, and the execution is slow
Properties of expectation and variance
Use graalvm native image to quickly expose jar code as a native shared library
Codeforces Round #771 (Div. 2)(A-C)
测试人员如何做不漏测?这7点就够了
For (Auto A: b) and for (Auto & A: b) usage
Automated video production
有时候只查询一行语句,执行也慢
Cs5268 perfectly replaces ag9321mcq typec multi in one docking station solution
AcWing 1135. Happy New Year (shortest path + search)
「 工业缺陷检测深度学习方法」最新2022研究综述
[cloud native topic -49]:kubesphere cloud Governance - operation - step by step deployment of microservice based business applications - basic processes and steps
数据库模式笔记 --- 如何在开发中选择合适的数据库+关系型数据库是谁发明的?
What are the benefits of multi terminal applet development? Covering Baidu applet, Tiktok applet, wechat applet development, and seizing the multi platform traffic dividend
Educational codeforces round 129 (rated for Div. 2) supplementary problem solution
burp 安装 license key not recognized
Postman download and installation
API文档工具knife4j使用详解