当前位置:网站首页>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 !
边栏推荐
- 笔记本安装TIA博途V17后出现蓝屏的解决办法
- 【Hot100】22. 括号生成
- AcWing 1128. Messenger solution (shortest path Floyd)
- Common problems and description of kt148a voice chip IC development
- [871. Minimum refueling times]
- [internship] solve the problem of too long request parameters
- Postman接口测试实战,这5个问题你一定要知道
- AcWing 1135. Happy New Year (shortest path + search)
- Basic concept of database, installation and configuration of database, basic use of MySQL, operation of database in the project
- Codeforces round 651 (Div. 2) (a thinking, B thinking, C game, D dichotomy, e thinking)
猜你喜欢
Review of the latest 2022 research on "deep learning methods for industrial defect detection"
[internship] solve the problem of too long request parameters
蓝牙芯片ble是什么,以及该如何选型,后续技术发展的路径是什么
Wu Enda's machine learning mind mapping insists on clocking in for 23 days - building a knowledge context, reviewing, summarizing and replying
Notes on hardware design of kt148a voice chip IC
HDL design peripheral tools to reduce errors and help you take off!
CRM客户关系管理系统
「 工业缺陷检测深度学习方法」最新2022研究综述
励志!大凉山小伙全奖直博!论文致谢看哭网友
测试人员如何做不漏测?这7点就够了
随机推荐
「 工业缺陷检测深度学习方法」最新2022研究综述
[daily question] 241 Design priorities for operational expressions
分享几个图床网址,便于大家分享图片
AcWing 340. Solution to communication line problem (binary + double ended queue BFS for the shortest circuit)
Google Earth Engine(GEE)——Landsat 9影像全波段影像下载(北京市为例)
GCC: Graph Contrastive Coding for Graph Neural NetworkPre-Training
Postman接口测试实战,这5个问题你一定要知道
在网上炒股开户安全吗?我是新手,还请指导
KT148A语音芯片ic的用户端自己更换语音的方法,上位机
Jetson XAVIER NX上ResUnet-TensorRT8.2速度与显存记录表(后续不断补充)
【Hot100】23. 合并K个升序链表
API文档工具knife4j使用详解
Kt148a voice chip IC user end self replacement voice method, upper computer
【871. 最低加油次数】
[Chongqing Guangdong education] reference materials for labor education of college students in Nanjing University
【QT】QPushButton创建
What are the benefits of multi terminal applet development? Covering Baidu applet, Tiktok applet, wechat applet development, and seizing the multi platform traffic dividend
CRM Customer Relationship Management System
In depth understanding of modern web browsers (I)
Automatic reading of simple books