当前位置:网站首页>Use of MySQL variables
Use of MySQL variables
2022-06-25 05:35:00 【Which floor do you rate moto】
Window functions can be viewed by clicking this link
- Error prone points of variable use
- about SET, have access to = or := As allocator
- Use SELECT Defining user variables can only use := As allocator ( Because in Africa SET In the sentence = Be treated as one ‘ Comparison operator ’.
- Through variables and if Statement to implement the window function (1):dense_rank()
SELECT
salary,
rk AS `RANK`
FROM
(
SELECT
salary,
IF
---- If @pres and salary equal , be @currank otherwise @currank be equal to @currank + 1
( @pres = salary, @currank, @currank := @currank + 1 ) AS rk,
@pres := salary
FROM
employee,(
SELECT
---- Declare variables @pres by -1,@currank by 0
@pres := - 1,
@currank := 0
) c1
ORDER BY salary DESC ) s
- Through variables and case when Statement to implement the window function (2):dense_rank()
SELECT
salary,
( CASE WHEN @pres = salary THEN @currank WHEN @pres := salary THEN @currank := @currank + 1 END ) AS `RANK`
FROM
employee,
( SELECT @pres := - 1, @currank := 0 ) AS c1
ORDER BY
salary DESC;
- adopt Count And DISTINCT Implement window functions (3):dense_rank()
SELECT
s.salary,
( SELECT COUNT( DISTINCT salary ) FROM employee e WHERE e.salary >= s.salary ) AS c1
FROM
employee s
ORDER BY
salary DESC;
- adopt group by,order by And variable implementation window function (4):dense_rank()
select
s.salary,
r.rank
from
employee s,
(
select
tmp1.salary,
(@rownum:[email protected]+1) as `Rank`
from
(select salary from employee group by salary order by salary desc) as tmp1,
(select @rownum:=0) tmp2
) as r
where
s.salary=r.salary
order by s.salary desc;
- adopt if Function to solve the following deformation problem

SELECT
DISTINCT s.Num
FROM
(
SELECT
t.*,
IF
( @before_num = t.Num, @times := @times + 1, 1 ) AS times,
@before_num := t.Num
FROM
( SELECT @before_num := NULL, @times := 1 ) r,
test t
) s
WHERE s.times >=3
边栏推荐
- Electric store stores data
- Database query optimization method
- Even if you are not good at anything, you are growing a little bit [to your 2021 summary]
- CTFHub-rce
- Double recursion in deep analysis merge sort
- SQL lab range explanation
- The k-th node of the binary search tree [sword finger offer]
- Depth of binary tree
- Transformations of pytorch torch torch vision
- TeeChart Pro ActiveX 2022.1
猜你喜欢

Activereportsjs V3.0 comes on stage

CTFHUB SSRF

Detailed summary of float

I got to know data types and function variables for the first time. I learned data types and function variables together today and yesterday, so I saved them in the first issue to record.

Detailed summary of flex layout

Array and simple function encapsulation cases

In depth understanding of line height and vertical align
![[Huawei machine test] hj16 shopping list](/img/54/d28f5aea9350af7797ca7c069e564d.jpg)
[Huawei machine test] hj16 shopping list

TeeChart Pro ActiveX 2022.1

Common cluster deployment schemes in redis
随机推荐
Characteristics of ES6 arrow function
JSON Library Tutorial from scratch (I): starting to learn and organize notes
Prototypical Networks for Few-shot Learning
Duplicate symbols for architecture i386 clang
Rce code execution & command execution (V)
JS handwriting depth clone array and object
Makefile Foundation
渗透测试-提权专题
A brief talk on media inquiry
How to choose the years of investment in financial products?
Array: force deduction dichotomy
Install pytorch through pip to solve the problem that torch cannot be used in jupyter notebook (modulenotfoundererror:no module named 'Torch').
C style string
Only these four instructions are required to operate SQL data
2022.1.23 diary
Could not find “store“ in the context of “Connect(homePage)
电子协会 C语言 1级 28 、字符菱形
"APEC industry +" biov Tech talks about the cross-border of Chinese biotechnology enterprises and "Pratt & Whitney Kangyu" | apec-hub public welfare
Bind simulation, key points of interpreting bind handwritten code [details]
Detailed summary of flex layout