Detailed explanation of the use of custom parameters in MySQL

Detailed explanation of the use of custom parameters in MySQL

MySQL variables include system variables and system variables. The learning task this time is user-defined variables. User variables mainly include local variables and session variables.

The declaration method of user-defined variables is as follows: @var_name, where the variable name consists of letters, numbers, ".", "_", and "$". Of course, you can include other characters when referring to a string or identifier (for example: @'my-var', @"my-var", or @my-var).

User-defined variables are session-level variables. The scope of its variables is limited to the client link in which it is declared. When the client disconnects, all its session variables will be released.

User-defined variables are not case-sensitive.

Use the SET statement to declare user-defined variables:

SET @my_var = 1; 
SET @my_var := 1;

When not using set, use := assignment because using = may be considered as a comparison operator.

The following is an example:

Write a SQL query to implement score ranking. If two scores are the same, then the two score ranks should be the same. Note that the next rank number after a tie should be the next consecutive integer value. In other words, there should be no "holes" between the rows and columns.

+—-+——-+
| Id | Score |
+—-+——-+
| 1 | 3.50 |
| 2 | 3.65 |
| 3 | 4.00 |
| 4 | 3.85 |
| 5 | 4.00 |
| 6 | 3.65 |
+—-+——-+

For example, given the Scores table above, your query should produce the following report (sorted by highest score):

+——-+——+
| Score | Rank |
+——-+——+
| 4.00 | 1 |
| 4.00 | 1 |
| 3.85 | 2 |
| 3.65 | 3 |
| 3.65 | 3 |
| 3.50 | 4 |
+——-+——+

Query statement:

select Score, @rank := @rank + (@pre <> (@pre:=Score)) Rank 
from Scores ,(SELECT @rank := 0,@pre := -1) INIT 
ORDER BY Score DESC

Note:

@rank indicates the ranking of grades

@pre indicates the score of the previous person

When the score is different from the previous one, @rank = @rank + 1, otherwise, @rank = rank.

Initialize @rank to 1 and @pre to -1.

The experimental results are:

The above detailed explanation of the use of custom parameters in MySQL is all the content that the editor shares with you. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM.

You may also be interested in:
  • Detailed explanation of the definition and usage of MySQL stored functions (custom functions)
  • Detailed explanation of the entry-level use of MySql stored procedure parameters
  • Detailed explanation of MYSQL configuration parameter optimization

<<:  Vue.js implements timeline function

>>:  Analysis of Linux boot system methods

Recommend

Use of marker tags in CSS list model

This article mainly introduces the ::master pseud...

VS2019 connects to mysql8.0 database tutorial with pictures and text

1. First, prepare VS2019 and MySQL database. Both...

Research on the problem of flip navigation with tilted mouse

In this article, we will analyze the production of...

Installation tutorial of MySQL 5.7.17 zip package version under win10

The installation tutorial of mysql5.7.17 is share...

Vue3 encapsulates the side navigation text skeleton effect component

Vue3 project encapsulation side navigation text s...

Do you know the weird things in Javascript?

Our veteran predecessors have written countless c...

A brief discussion on VUE uni-app conditional coding and page layout

Table of contents Conditional compilation Page La...

mysql having usage analysis

Usage of having The having clause allows us to fi...

How to add rounded borders to div elements

As shown below: CSS CodeCopy content to clipboard...

MySQL database architecture details

Table of contents 1. MySQL Architecture 2. Networ...

One line of code solves various IE compatibility issues (IE6-IE10)

x-ua-compatible is used to specify the model for ...

Storage engine and log description based on MySQL (comprehensive explanation)

1.1 Introduction to storage engines 1.1.1 File sy...

Docker image optimization (from 1.16GB to 22.4MB)

Table of contents The first step of optimization:...