当前位置:网站首页>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 !
边栏推荐
- Shardingsphere jdbc5.1.2 about select last_ INSERT_ ID () I found that there was still a routing problem
- KT148A语音芯片ic的开发常见问题以及描述
- [JS] get the search parameters of URL in hash mode
- sense of security
- Detailed explanation of VBScript (I)
- 自动生成VGG图像注释文件
- [cloud native topic -50]:kubesphere cloud Governance - operation - step by step deployment of microservice based business applications - database middleware MySQL microservice deployment process
- Attack and defense world PWN question: Echo
- 笔记本安装TIA博途V17后出现蓝屏的解决办法
- Implementation of online shopping mall system based on SSM
猜你喜欢

How can testers do without missing tests? Seven o'clock is enough

Review of the latest 2022 research on "deep learning methods for industrial defect detection"

【Hot100】21. Merge two ordered linked lists

B端电商-订单逆向流程

C language linked list -- to be added

Attack and defense world PWN question: Echo

Design and implementation of ks004 based on SSH address book system

Summary of interview experience, escort your offer, full of knowledge points

Basic concept of database, installation and configuration of database, basic use of MySQL, operation of database in the project

pytorch 模型保存的完整例子+pytorch 模型保存只保存可训练参数吗?是(+解决方案)
随机推荐
Automated video production
HDL design peripheral tools to reduce errors and help you take off!
Driverless learning (4): Bayesian filtering
【JS】获取hash模式下URL的搜索参数
笔记本安装TIA博途V17后出现蓝屏的解决办法
Attack and defense world PWN question: Echo
想请教一下,究竟有哪些劵商推荐?手机开户是安全么?
Common problems and description of kt148a voice chip IC development
Detailed explanation of VBScript (I)
[QT] QPushButton creation
NMF-matlab
Cron expression (seven subexpressions)
在券商账户上买基金安全吗?哪里可以买基金
[JS] get the search parameters of URL in hash mode
KT148A语音芯片ic的软件参考代码C语言,一线串口
RPD出品:Superpower Squad 保姆级攻略
Taiwan SSS Xinchuang sss1700 replaces cmmedia cm6533 24bit 96KHz USB audio codec chip
GCC: Graph Contrastive Coding for Graph Neural NetworkPre-Training
How can testers do without missing tests? Seven o'clock is enough
AcWing 1126. Minimum cost solution (shortest path Dijkstra)