mysql 查询变量@i:=@i+1

        学习完mysql的查询:基本查询连接查询和子查询和mysql 正则表达式查询,接下来先学习下变量查询。

        mysql中没有oracle序列号那一列。mysql可以使用查询变量的方式去处理。我们先了解下查询变量,后面应用起来就更清晰。

1,定义

        MySQL查询变量是指在MySQL查询语句中使用的变量,可以在查询语句中定义和使用,用于存储和传递数据。查询变量可以是用户定义变量或系统变量,用户定义变量需要使用@符号来表示,系统变量则不需要。

2,使用语法

        MySQL查询变量可以在查询语句中定义和使用,定义时需要使用SET语句,格式如下:

SET @变量名 = 值;

1,系统变量:

1,局部修改(会话级别):只针对当前自己客户端当次连接有效

语法:

set 变量名= 新值;

比如:

-- 设置查询变量
SET @myVariable := 0; 
-- 使用查询变量进行操作
SELECT @myVariable := @myVariable + 1 AS counter; 
-- 重置查询变量
SET @myVariable := 0;

查看变量名:

语法:

select @变量名;

比如:

SELECT @myVariable;

2,全局修改:针对所有的客户端,“所有时刻都”有效

语法:

set global 变量名 = 值;

        这个很少去操作这。更多用局部变量。

2,select 单个的:

        在from 后面初始变量值,声明的话,select那边就可以

SELECT (@i:=@i+1) AS 序号 FROM (SELECT @i:=0) AS temp;

3,局部变量

        作用范围在begin到end语句块之间,在该语句块里设置的变量

        declare语句专门用于定义局部变量

语法:

declare 变量名 数据类型 [属性];

        1、局部变量使用declare关键字声明,(没有该语句则不视为局部变量)

        2、局部变量declare语句出现的位置一定是在begin和end之间(begin end是在大型语句块中使用:函数/存储过程/触发器)

3,作用:

        MySQL查询变量可以用于存储和传递数据,常用于条件查询、流程控制等方面。

        例如,可以使用查询变量实现动态条件查询:

SET @id = 18;
SELECT * FROM 表名 WHERE id = @id;

        可以使用查询变量实现条件判断:

SET @flag = 1;
IF @flag = 1 THEN
SELECT * FROM 表名;
SELECT * FROM 表名 WHERE 列名 = 值;
END IF;

4,优缺点:

        MySQL查询变量的优点是可以提高查询效率,避免重复查询,同时可以实现动态查询和条件判断等功能。

        缺点是使用不当可能导致查询结果不准确,同时需要注意变量的作用域和生命周期,避免出现变量值被覆盖或未定义的情况。

        MySQL查询变量是一种在查询语句中定义和使用的变量,可以用于存储和传递数据,常用于条件查询、流程控制等方面。使用查询变量可以提高查询效率,避免重复查询,同时可以实现动态查询和条件判断等功能。但需要注意变量的作用域和生命周期,避免出现变量值被覆盖或未定义的情况。

5,应用:

0,数据准备:

-- 建表
DROP TABLE IF EXISTS employee;
CREATE TABLE employee(
  id INT NOT NULL AUTO_INCREMENT,
  NAME VARCHAR(10),
  salary DECIMAL(10,2),
  dept_id INT DEFAULT 0,
  PRIMARY KEY(id)
);


-- 插入数据:
INSERT INTO employee VALUES
(1,'jack', 7000, 1),
(2, 'tom', 8000, 1),
(3, 'joe', 8000, 1),
(4, 'dell', 9000, 2),
(5, 'ken', 10000, 2),
(6, 'tim', 6000, 3),
(7, 'steven', 7000, 3),
(8, 'tank', 9000, 1),
(9, 'tolly', 10000, 1),
(10, 'jony', 12000, 1),
(12, 'jenny', 12000, 2),
(15, 'jams', 13000, 3),
(19, 'west', 16000, 3);

1,查询序号:

SELECT (@i:=@i+1) AS 序号, id, NAME
FROM employee, (SELECT @i:=0) AS temp;

查询结果:

2,统计数量

        由上面的序号可以知道数量,直接再获取其最大值:

SELECT MAX(count_num) count_num FROM (
SELECT (@count:=@count+1) AS count_num
FROM employee, (SELECT @count:=0) AS temp
) AS temp2;

3,求和

        统计上面id的和的值

SELECT MAX(sum_num) sum_num FROM (
SELECT (@count:=@count+e.id) AS sum_num
FROM employee e, (SELECT @count:=0) AS temp
) AS temp2;

4,统计各部门的人数

直接sql:
SELECT dept_id, COUNT(1) AS staff_num 
FROM employee e GROUP BY e.dept_id;

用变量的方式:

        处理思路: 要根据dept_id聚合,一个变量肯定不够,要声明一个变量pre,赋值dept_id。

如果pre变量的值,跟dept_id值是一样的,则count加1

sql:

SELECT *, @count:=IF(@pre=dept_id, @count+1, 1) AS cout_num, 
  @pre:=dept_id AS pre 
FROM employee,(SELECT @count:=0, @pre:=NULL) v;

        由结果可知,因为dept_id是分散的,计算值是根据顺序来的。

        再加一层排序。

SELECT *, @count:=IF(@pre=dept_id,@count+1,1) AS cout_num, 
  @pre:=dept_id AS pre 
FROM employee,(SELECT@count:=0, @pre:=NULL) v
ORDER BY dept_id;

        这样结果就符合初步想要的了。

        然后再进行过滤下:

 SELECT dept_id, MAX(cout_num) AS staff_num FROM (
  SELECT dept_id, @count:=IF(@pre=dept_id,@count+1,1) AS cout_num, 
    @pre:=dept_id AS pre 
  FROM employee,(SELECT@count:=0, @pre:=NULL) v
  ORDER BY dept_id
) AS temp3 GROUP BY dept_id;

总结:

        mysql查询变量很强大,mysql没有序列号的,就可以直接用这种方式。 还有其它很多应用的地方,后续有遇到再补充。

        上一篇: 《mysql 正则表达式查询-CSDN博客

        下一篇: 《mysql 变量查询题目

  • 45
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
This is a MySQL query that selects the student ID (s_id), assigns a sequential number to each row (i), and calculates the rank of each student based on their sum of scores (sum_score). The query uses a subquery to first group the scores by student ID and calculate the sum of scores for each student. This subquery is then joined with a variable initialization subquery that sets the initial values of @k, @i, and @score to 0. The variable @k is used to keep track of the current rank while iterating over the rows. The variable @i is used to assign a sequential number to each row. The variable @score is used to compare the sum_score of the current row with the sum_score of the previous row. The CASE statement is used to check if the sum_score of the current row is equal to the sum_score of the previous row. If they are equal, then the rank remains the same. If they are not equal, then the rank is updated to the current sequential number. Here is a breakdown of the query: 复制 SELECT a.s_id, -- Select the student ID @i:=@i+1 AS i, -- Assign a sequential number to each row @k:=(case when @score=a.sum_score then @k else @i end) as rank, -- Calculate the rank a.sum_score AS score -- Select the sum of scores for each student FROM (SELECT s_id,SUM(s_score) AS sum_score FROM score GROUP BY s_id ORDER BY sum_score DESC) a, -- Subquery to calculate sum of scores for each student (SELECT @k:=0,@i:=0,@score:=0) s -- Subquery to initialize variables Note that the use of variables in this query is not recommended, as it can lead to unexpected results if the variables are not reset properly. It is better to use a subquery or a window function to calculate the rank. 翻译
02-27
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天狼1222

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值