当前位置:网站首页>The role of the symbol @ in MySQL
The role of the symbol @ in MySQL
2022-06-27 15:45:00 【1024 Q】
mysql Of @ usage
actual combat
select @a;
Variable name , If you don't , You will think that this is a column name , But this column doesn't exist , It's a mistake ;
@ Variable name : Define a user variable .
= Assign a value to the user variable .
There are two ways to assign user variables : One is direct use "=“ Number , The other is to use ”:=“ Number .
The difference is :
Use set When the command assigns a value to a user variable , Both methods can be used ;
use select When the sentence is , Only use ”:=“ The way , because select In the sentence ,”=" The sign is considered a comparison operator .
(@i:[email protected]+1)
You can generate a set of incremental serial numbers when generating the query result table
select (@i:[email protected]+5) as rownum, surname, personal_name from student, (select @i:=100) as init;
select @ids := 101,@l := 0
GROUP_CONCAT + group by
Group by , Connect the data on the field , Default to ,, You can also specify a delimiter
mysql Of @ usage1, Add temporary table , Self increment of variables
SELECT (@i:[email protected]+1),t.* FROM table_name t,(SELECT @i:=0) AS j(@i:[email protected]+1) Represents the definition of a variable , Every time you stack 1;
(SELECT @i:=0) AS j For creating a temporary table ,j Is a random table name , But aliases must be .
2, Implement sort increment
SELECT( @i := @i + 1 ),p.* FROM( SELECT * FROM sys_region ORDER BY create_time DESC ) p, ( SELECT @i := 0 ) k
3, Realize group increment
[email protected] := CASE WHEN @type = a.LEVEL THEN @r + 1 ELSE 1 END AS rowNum,@type := a.`level` AS type,a.id FROMsys_region a,( SELECT @r := 0, @type := '' ) b;
Temporary variables are used here @type, Because when operating on temporary variables , The variables used are all temporary variables .
actual combatINSERT INTO t_top ( col_account, col_date, p_account, p_name )SELECT b.col_account, b.col_date, b.p_account, b.p_name FROM([email protected] := CASE WHEN @account = a.col_account THEN @num + 1 ELSE 1 END AS rownum,@account := a.col_account AS account,a.* FROM( SELECT * FROM zb_top ORDER BY col_account, p_avg DESC ) a,( SELECT @num := 0, @account := '' ) j) b where 6 > b.rownumCalculate the number of days since the user last visited , according to imei Number to distinguish different users , If there is only one visit in the period, it is 0.
Initialization data :
Code example :
CREATE TABLE `pd` (`imei` varchar(32) NOT NULL DEFAULT '',`date` datetime DEFAULT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8;INSERT INTO `pd` VALUES ('1', '2013-07-25 00:00:01');INSERT INTO `pd` VALUES ('1', '2013-07-26 00:00:02');INSERT INTO `pd` VALUES ('2', '2013-07-23 00:00:04');INSERT INTO `pd` VALUES ('2', '2013-07-26 00:00:03');INSERT INTO `pd` VALUES ('3', '2013-07-26 00:00:01'); Script , Use @ Special variables :
Code example :
SELECT * FROM(SELECTimei user_id,max(max_dd),max(max_dd_2),to_days(max(max_dd)) - to_days(max(max_dd_2)) days FROM(SELECTimei,max_dd,max_dd_2 FROM(SELECTtmp.imei,tmp.date,IF(@imei = tmp.imei, @rank := @rank + 1, @rank := 1) AS ranks,IF(@rank = 1, @max_d := tmp.date, @max_d := NULL) AS max_dd,IF(@rank = 2, @max_d_2 := tmp.date, @max_d_2 := NULL) AS max_dd_2,@imei := tmp.imei FROM(SELECT imei, date FROM pd ORDER BY imei ASC, date DESC) tmp,(SELECT @rownum := 0, @imei := NULL, @rank := 0, @max_d := NULL, @max_d_2 := NULL) a ) result ) t GROUP BYimei HAVINGcount(*) > 1 ) x WHERE x.days >= 1 AND EXISTS (SELECT 'x' FROM pd WHERE date > '2013-07-26 00:00:00')
Be careful :
When there is a large amount of data in the table , Use union all Such operations will have tragic results .
This is about MySQL Middle symbol @ This is the end of the article on the role of , More about MySQL Symbol @ Please search the previous articles of software development network or continue to browse the relevant articles below. I hope you will support software development network more in the future !
边栏推荐
- Does polardb-x currently not support self-made database service Das?
- Keep valid digits; Keep n digits after the decimal point;
- E ModuleNotFoundError: No module named ‘psycopg2‘(已解决)
- 分布式Session解决方案
- How is the London Silver point difference calculated
- 一场分销裂变活动,不止是发发朋友圈这么简单!
- Design of spread spectrum communication system based on FPGA (with main code)
- Design of UART controller based on FPGA (with code)
- 洛谷_P1003 [NOIP2011 提高组] 铺地毯_暴力枚举
- [kotlin] the next day
猜你喜欢

洛谷入门2【分支结构】题单题解
![洛谷_P1003 [NOIP2011 提高组] 铺地毯_暴力枚举](/img/65/413ac967cc8fc22f170c8c7ddaa106.png)
洛谷_P1003 [NOIP2011 提高组] 铺地毯_暴力枚举

HTTP Caching Protocol practice

Atomic operation class

Open source 23 things shardingsphere and database mesh have to say

分布式Session解决方案
![洛谷_P1002 [NOIP2002 普及组] 过河卒_dp](/img/80/4edf21e0ac316fe3dd727159621acb.png)
洛谷_P1002 [NOIP2002 普及组] 过河卒_dp

All you want to know about large screen visualization is here

Admixture usage document Cookbook

PSS:你距离NMS-free+提点只有两个卷积层 | 2021论文
随机推荐
What is the London Silver unit
[interview questions] common interview questions (I)
What is the London Silver code
可变参数模板 Variadic Templates
volatile与JMM
FPGA based analog I ² C protocol system design (with main code)
洛谷_P1003 [NOIP2011 提高组] 铺地毯_暴力枚举
Fundamentals of software engineering (I)
Vscode uses yapf auto format to set the maximum number of characters per line
HTTP Caching Protocol practice
On traversal of tree nodes
避孕套巨头过去两年销量下降40% ,下降原因是什么?
[high concurrency] deeply analyze the callable interface
Design of digital video signal processor based on FPGA (with main code)
Cesium 使用MediaStreamRecorder 或者MediaRecorder录屏并下载视频,以及开启摄像头录像。【转】
Let's talk about the process of ES Indexing Documents
带你认识图数据库性能和场景测试利器LDBC SNB
Go error collection | when a function uses a return value with a parameter name
固收+产品有什么特点?
How is the London Silver point difference calculated