当前位置:网站首页>Experience of Niuke SQL
Experience of Niuke SQL
2022-07-07 06:09:00 【ZMC Xi】
Cattle from SQL Experience of topic brushing
Topic link : Cattle from SQL piece
Ranking : Can pass select To implement , That is to define a variable , So as to realize each accumulation :
select @t_rank:=0, Through this, we can realize that the ranking will not repeat because of the same score ; Corresponding ones with the same ranking due to the same score can also be achieved through this , That is, through Two variables , One to store the ranking , One to store scores ( To achieve the same score , For the same place ), The specific way of writing :select grade, case when @mid=grade then @cnt when @mid:=grade then @cnt:[email protected]+1 end t_rank from grades,(select @cnt:=0,@mid:=null) r order by grade desc;In this way , Be careful
:=and=The difference betweenBut in MySQL8.0 Added window function , It can easily solve the above problems :
row_number()dense_rank()rank()The first is general sorting ; The second is the sort with the same score , There is no separation ; The third is the sort with the same score , But there are gaps
usage :
dense_rank() over(order by xxxxx)Other uses are similarRelated cumulative problems : That is, the sum of the current field plus the data of the previous field
- You can also take define Variable to solve , But this will always make some inexplicable mistakes ( It's not that the answer is incorrect )
- You can use the method of table connection , Use a table as the main table , A supplementary table , Some non equivalent relations can be used to realize
- You can use the open window function :
sum( Accumulated fields ) over(order by xxxxxx)This method is very simple
alter Create index :
- Primary key :
alter table table_name add primary key (column_name); - only :
alter table table_name add unique index_name (column_name); - alter Create multiple indexes :
alter table table_name add .......,add.......;
- Primary key :
Force index :
force index(index_name), Usually occurs in from After table nameAdd fields to the created table :
alter table table_name add column Back and create Create a table ;replace(A,B,C): It means will a Used in b String use c Instead of
Add foreign key constraints to the created table :
alter table table_name add constraint foreign key ( Field ) references Table name ( Field name );, Notice the parentheses hereIn the use of update While updating the data , If the updated data is limited to a field of another table , Then you can use
inKeyword to implement ,in You can add data of a field laterModify the name of the table :
alter table table_name rename to/as new_table_name;inandexistsThe difference between :in:in Whether the query matches the subquery or the existing record ; Execute subquery first , Then form a Cartesian product with the appearance , To match each other .in It will only be queried onceexists:exists Is to query whether this record exists , It doesn't pay attention to the fields of the query , Only care about whether this field exists . First, traverse the surface , Then match with the inner table , If it exists , Add the record to the result set ; Usually, there are two tables in the sub query , There is only one table that will make mistakes
Query when a string appears in a field :
(length(column_name)-length(replace(column_name,'xxx','')))/ The length of a single string ;see MySQL Ranking of records in each group in , You can use the open window function , Just sort those , Only need over Add a
partition by Grouping fieldthat will do ,partition by It is usually used in conjunction with order byMySQL Returns the month :
month()If you want to connect all the data in the Group , Use
group_concat( Fields to be connected ,'x'),x Usually connected punctuation , Usually,; This function also needs cooperation group by To useMySQL in 3 Insert in :
insert into: Means insert data ,database Will check the primary key, When the inserted data is repeated , You're going to report a mistakereplace into: Represents inserting and replacing data , In the demand table There is primary key or unique when , If database The record already exists , The data will be replaced ; If there is no , Simply insertinsert ignore into: Indicates that if the inserted data is repeated , Will ignore the current new data ,( Presence in table primary key or unique)
While deleting data , Cannot find at the same time , You need to use the middle table
trigger : Triggered by events , Can only be created in a permanent table , Cannot create in view or In the temporary table ,
Format :
create trigger trigger_name trigger_time trigger_event on table_name for each row trigger_body;trigger_time: Trigger time ,after or before
trigger_event: Triggering event ,insert update delete
trigger_body: Trigger subject , Can combine begin…end Compound statements to use , Because the trigger subject is also SQL sentence , Also need to
;, have access to old and new To reference the record content that occurs in the trigger- old: Express delete and update, Delete or update previous data
- new: Express update and insert, New data updated or inserted
The specific use
old.column_name
边栏推荐
- Go language learning notes - Gorm use - native SQL, named parameters, rows, tosql | web framework gin (IX)
- Jmeter自带函数不够用?不如自己动手开发一个
- On the discrimination of "fake death" state of STC single chip microcomputer
- 牙齿干细胞的存储问题(未完待续)
- Type de texte de commutation d'entrée et de mot de passe de l'applet natif
- @pathvariable 和 @Requestparam的详细区别
- yarn入门(一篇就够了)
- Industrial Finance 3.0: financial technology of "dredging blood vessels"
- JVM命令之 jinfo:实时查看和修改JVM配置参数
- Financial risk control practice - decision tree rule mining template
猜你喜欢

VScode进行代码补全

From "running distractor" to data platform, Master Lu started the road of evolution

Deep clustering: joint optimization of depth representation learning and clustering
![A freshman's summary of an ordinary student [I don't know whether we are stupid or crazy, but I know to run forward all the way]](/img/fd/7223d78fff54c574260ec0da5f41d5.png)
A freshman's summary of an ordinary student [I don't know whether we are stupid or crazy, but I know to run forward all the way]

驱动开发中platform设备驱动架构详解

Red Hat安装内核头文件

Check Point:企业部署零信任网络(ZTNA)的核心要素

Vscode for code completion
![[SQL practice] a SQL statistics of epidemic distribution across the country](/img/ba/639a23d87094d24572a69575b565b9.png)
[SQL practice] a SQL statistics of epidemic distribution across the country

Financial risk control practice - decision tree rule mining template
随机推荐
A freshman's summary of an ordinary student [I don't know whether we are stupid or crazy, but I know to run forward all the way]
Vscode for code completion
Bbox regression loss function in target detection -l2, smooth L1, IOU, giou, Diou, ciou, focal eiou, alpha IOU, Siou
Deep clustering: joint optimization of depth representation learning and clustering
Classic questions about data storage
当我们谈论不可变基础设施时,我们在谈论什么
If you don't know these four caching modes, dare you say you understand caching?
苹果cms V10模板/MXone Pro自适应影视电影网站模板
Interview questions and salary and welfare of Shanghai byte
Check point: the core element for enterprises to deploy zero trust network (ztna)
Introduction to yarn (one article is enough)
Bat instruction processing details
Flask1.1.4 werkzeug1.0.1 source code analysis: start the process
产业金融3.0:“疏通血管”的金融科技
Sequential storage of stacks
10W word segmentation searches per second, the product manager raised another demand!!! (Collection)
深度聚类:将深度表示学习和聚类联合优化
成为资深IC设计工程师的十个阶段,现在的你在哪个阶段 ?
牙齿干细胞的存储问题(未完待续)
404 not found service cannot be reached in SAP WebService test