当前位置:网站首页>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

  1. 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 between

    But 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 similar

  2. Related 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
  3. 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.......;
  4. Force index :force index(index_name), Usually occurs in from After table name

  5. Add fields to the created table :alter table table_name add column Back and create Create a table ;

  6. replace(A,B,C): It means will a Used in b String use c Instead of

  7. 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 here

  8. In 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 later

  9. Modify the name of the table :alter table table_name rename to/as new_table_name;

  10. in and exists 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 once
    • exists: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
  11. Query when a string appears in a field :(length(column_name)-length(replace(column_name,'xxx','')))/ The length of a single string ;

  12. 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 by

  13. MySQL Returns the month :month()

  14. 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 use

  15. MySQL 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 mistake
    • replace 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 insert
    • insert ignore into: Indicates that if the inserted data is repeated , Will ignore the current new data ,( Presence in table primary key or unique)
  16. While deleting data , Cannot find at the same time , You need to use the middle table

  17. 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

原网站

版权声明
本文为[ZMC Xi]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202130721525889.html