当前位置:网站首页>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
边栏推荐
- 职场经历反馈给初入职场的程序员
- On the difference between FPGA and ASIC
- Solve pod install error: FFI is an incompatible architecture
- k8s运行oracle
- cf:C. Column Swapping【排序 + 模拟】
- JVM命令之 jstat:查看JVM統計信息
- SQL Server 2008 各种DateTime的取值范围
- Interview questions and salary and welfare of Shanghai byte
- Subghz, lorawan, Nb IOT, Internet of things
- [daily training -- Tencent selected 50] 292 Nim games
猜你喜欢
CTFshow--常用姿势
PTA ladder game exercise set l2-004 search tree judgment
The boss always asks me about my progress. Don't you trust me? (what do you think)
产业金融3.0:“疏通血管”的金融科技
cf:C. Column Swapping【排序 + 模拟】
SubGHz, LoRaWAN, NB-IoT, 物联网
Chain storage of stack
Red Hat安装内核头文件
JVM monitoring and diagnostic tools - command line
ML之shap:基于adult人口普查收入二分类预测数据集(预测年收入是否超过50k)利用shap决策图结合LightGBM模型实现异常值检测案例之详细攻略
随机推荐
Career experience feedback to novice programmers
PowerPivot - DAX (function)
Nvisual network visualization
Financial risk control practice - decision tree rule mining template
Convert numbers to string strings (to_string()) convert strings to int sharp tools stoi();
Sequential storage of stacks
Interview questions and salary and welfare of Shanghai byte
Interview skills of software testing
Vscode for code completion
[daily training -- Tencent selected 50] 292 Nim games
C. colonne Swapping [tri + Simulation]
What EDA companies are there in China?
[solved] record an error in easyexcel [when reading the XLS file, no error will be reported when reading the whole table, and an error will be reported when reading the specified sheet name]
Harmonyos practice - Introduction to development, analysis of atomized services
关于STC单片机“假死”状态的判别
PowerPivot——DAX(函数)
PTA 天梯赛练习题集 L2-003 月饼 测试点2,测试点3分析
JVM命令之 jstat:查看JVM统计信息
[云原生]微服务架构是什么?
STM32按键状态机2——状态简化与增加长按功能