当前位置:网站首页>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 !
边栏推荐
- 勵志!大凉山小夥全獎直博!論文致謝看哭網友
- upload-labs
- Spark source code compilation, cluster deployment and SBT development environment integration in idea
- 数据库模式笔记 --- 如何在开发中选择合适的数据库+关系型数据库是谁发明的?
- 现在券商的优惠开户政策什么?实际上网上开户安全么?
- 台湾SSS鑫创SSS1700替代Cmedia CM6533 24bit 96KHZ USB音频编解码芯片
- What is online account opening? Is it safe to open an account online now?
- Review of the latest 2022 research on "deep learning methods for industrial defect detection"
- [译]深入了解现代web浏览器(一)
- Codeforces Round #771 (Div. 2)(A-C)
猜你喜欢

【Hot100】21. Merge two ordered linked lists

Overview of browser caching mechanism

API documentation tool knife4j usage details

Kt148a voice chip IC user end self replacement voice method, upper computer

API文档工具knife4j使用详解

攻防世界pwn题:Recho
![[871. Minimum refueling times]](/img/5f/75e717d1fc9d1c5f9e1d8f59dda38c.png)
[871. Minimum refueling times]

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

CS5268完美代替AG9321MCQ Typec多合一扩展坞方案

「 工业缺陷检测深度学习方法」最新2022研究综述
随机推荐
AcWing 1126. Minimum cost solution (shortest path Dijkstra)
Kt148a voice chip IC software reference code c language, first-line serial port
AcWing 341. Optimal trade solution (shortest path, DP)
Attack and defense world PWN question: Echo
CRM Customer Relationship Management System
有时候只查询一行语句,执行也慢
攻防世界pwn题:Recho
[NLP] a detailed generative text Abstract classic paper pointer generator
【Hot100】22. 括号生成
RPD出品:Superpower Squad 保姆级攻略
upload-labs
Jetson XAVIER NX上ResUnet-TensorRT8.2速度与显存记录表(后续不断补充)
B端电商-订单逆向流程
KT148A语音芯片ic的用户端自己更换语音的方法,上位机
想请教一下,我在东莞,到哪里开户比较好?手机开户是安全么?
GCC: Graph Contrastive Coding for Graph Neural NetworkPre-Training
What are the benefits of multi terminal applet development? Covering Baidu applet, Tiktok applet, wechat applet development, and seizing the multi platform traffic dividend
Sometimes only one line of statements are queried, and the execution is slow
【每日一题】241. 为运算表达式设计优先级
Google Earth engine (GEE) - Landsat 9 image full band image download (Beijing as an example)