当前位置:网站首页>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
in
Keyword 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;
in
andexists
The 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 field
that 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
边栏推荐
- Bypass open_ basedir
- Convert numbers to string strings (to_string()) convert strings to int sharp tools stoi();
- Ctfshow-- common posture
- JVM监控及诊断工具-命令行篇
- MFC BMP sets the resolution of bitmap, DPI is 600 points, and gdiplus generates labels
- 关于STC单片机“假死”状态的判别
- PTA 天梯赛练习题集 L2-002 链表去重
- Reading notes of Clickhouse principle analysis and Application Practice (6)
- On the discrimination of "fake death" state of STC single chip microcomputer
- Harmonyos practice - Introduction to development, analysis of atomized services
猜你喜欢
Randomly generate session_ id
ML之shap:基于adult人口普查收入二分类预测数据集(预测年收入是否超过50k)利用shap决策图结合LightGBM模型实现异常值检测案例之详细攻略
PowerPivot - DAX (function)
Jstat of JVM command: View JVM statistics
C note 13
JVM监控及诊断工具-命令行篇
Vscode for code completion
Apple CMS V10 template /mxone Pro adaptive film and television website template
PTA 天梯赛练习题集 L2-004 搜索树判断
Opensergo is about to release v1alpha1, which will enrich the service governance capabilities of the full link heterogeneous architecture
随机推荐
[cloud native] what is the microservice architecture?
[InstallShield] Introduction
Flask 1.1.4 werkzeug1.0.1 analyse du code source: processus de démarrage
Understand the deserialization principle of fastjson for generics
PTA TIANTI game exercise set l2-003 moon cake test point 2, test point 3 Analysis
JVM监控及诊断工具-命令行篇
Nvisual network visualization
老板总问我进展,是不信任我吗?(你觉得呢)
PowerPivot - DAX (function)
On the discrimination of "fake death" state of STC single chip microcomputer
从“跑分神器”到数据平台,鲁大师开启演进之路
盘点国内有哪些EDA公司?
线性回归
ML之shap:基于adult人口普查收入二分类预测数据集(预测年收入是否超过50k)利用shap决策图结合LightGBM模型实现异常值检测案例之详细攻略
What is make makefile cmake qmake and what is the difference?
PTA 天梯赛练习题集 L2-003 月饼 测试点2,测试点3分析
目标检测中的损失函数与正负样本分配:RetinaNet与Focal loss
改变ui组件原有样式
How to improve website weight
Digital IC interview summary (interview experience sharing of large manufacturers)