mysql :=和=的区别及统计查询结果行号

变量初始化:@rownum:=0

累加,标记行号:@rownum:=@rownum+1

SELECT
	rank
FROM
	 (
		SELECT
			id,
			@rownum := CAST(@rownum AS SIGNED) + 1 AS rank 
		FROM
			(
				SELECT
					c.id,
					bcu.guid,
					COUNT(DISTINCT bcu.uguid) AS num 
				FROM
					`b_record` bcu 
				JOIN b_company c ON bcu.cguid = c.cguid 
				WHERE
					bcu.updatetime BETWEEN (NOW() - INTERVAL 148 HOUR)
				AND NOW()
				AND c.state = 1 
				GROUP BY
					cguid 
				ORDER BY
					num DESC
			) s,
			(SELECT @rownum := 0) r
	) t 
WHERE
	id = 123

完整代码如上图所示,其中

SELECT
					c.id,
					bcu.companyguid,
					COUNT(DISTINCT bcu.userguid) AS num 
				FROM
					`b_company_user_visit_record` bcu 
				JOIN b_company c ON bcu.companyguid = c.companyguid 
				WHERE
					bcu.updatetime BETWEEN (NOW() - INTERVAL 148 HOUR)
				AND NOW()
				AND c.state = 1 
				GROUP BY
					companyguid 
				ORDER BY
					num DESC

上面一段查出结果

SELECT
			id,
			@rownum := CAST(@rownum AS SIGNED) + 1 AS rank 
		FROM
			(
					) s,
			(SELECT @rownum := 0) r

上面一段添加行号,因为在java中,所以合并成一条语句,而在sql中可分开写,先定义rownum变量,再添加行号。

参考:

https://blog.youkuaiyun.com/wangjun5159/article/details/51378558

https://blog.youkuaiyun.com/u012102536/article/details/59112054

SELECT t1.sell_date, CONVERT(t1.num_sold, SIGNED) AS "num_sold", REPLACE(t1.products, '"', '') AS "products" FROM ( SELECT @rn := CASE WHEN @sell_date = a.sell_date THEN @rn+1 ELSE 1 END AS 'row_num', @num := CASE WHEN @sell_date = a.sell_date AND LOCATE(concat('"', a.product,'"'), @products)=0 THEN @num + 1 WHEN @sell_date = a.sell_date AND LOCATE(concat('"', a.product,'"'), @products)>0 THEN @num ELSE 1 END AS "num_sold", -- 加上分号是为了防止出现 Pen BallPen 这样的问题 @products := CASE WHEN @sell_date = a.sell_date AND LOCATE(concat('"', a.product,'"'), @products)=0 THEN concat(@products, ',','"', a.product,'"') WHEN @sell_date = a.sell_date AND LOCATE(concat('"', a.product,'"'), @products)>0 THEN @products ELSE concat('"', a.product,'"') END AS "products", @sell_date := a.sell_date AS "sell_date" FROM (SELECT * FROM Activities a ORDER BY sell_date, product) a, (SELECT @sell_date := NULL, @products:=NULL, @rn:=0, @num :=0) b ) t1 JOIN ( SELECT sell_date, max(row_num) AS "max_row_num" FROM ( SELECT @rn := CASE WHEN @sell_date = a.sell_date THEN @rn+1 ELSE 1 END AS 'row_num', @products := CASE WHEN @sell_date = a.sell_date AND LOCATE(concat('"', a.product,'"'), @products)=0 THEN concat(@products, ',','"', a.product,'"') WHEN @sell_date = a.sell_date AND LOCATE(concat('"', a.product,'"'), @products)>0 THEN @products ELSE concat('"', a.product,'"') END AS "products", @sell_date := a.sell_date AS "sell_date" FROM (SELECT * FROM Activities a ORDER BY sell_date) a, (SELECT @sell_date := NULL, @products:=NULL, @rn:=0, @num :=0) b ) t GROUP BY sell_date ) t2 ON t1.sell_date = t2.sell_date AND t1.row_num = t2.max_row_num;
最新发布
11-29
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值