mysql查询的结果表中在添加一列作为排序使用 @row_num

本文详细介绍了一种在SQL查询中实现动态排名的方法,并结合分页功能,确保排名的连贯性和准确性。通过使用变量和子查询,文章展示了如何在复杂的数据集中保持排序的一致性,即使在分页情况下也能正确显示排名。

// 在结果表后在加一列递增一 作为排名使用
		// 为了分页的时候排名是在整体数据上进行排序而不是每一页都是从1开始这个 @row_num应该是动态的 #{offset} 和limt #{offset} 一致
SET @row_num = 11;

// 由于查询的条件中有group BY 排序sort如果在里面的话会乱序 所以要在结果上进行添加 sort SELECT
	result.*, @row_num := @row_num + 1 AS sort
FROM
	( //普通的数据查询结果
		SELECT
			ui.user_id,
			ui.img,
			ui.nick_name,
			ui.sex,
			sum(ugal.numbers) giftCount
		FROM
			user_give_away_log ugal
		LEFT JOIN user_info ui ON ugal.user_id = ui.user_id
		WHERE
			ugal.is_type = 2
		AND ui.statues = 1
		AND ui.del = 1
		GROUP BY
			ugal.user_id
		ORDER BY
			giftCount DESC,
			CONVERT (ui.user_id, CHAR)
	) AS result // 分页要在结果之外 否则每页排名都是从1开始
LIMIT #{offset}, #{limit}

 

SELECT invono, date, vendorid, vendor, model, color, qty, weight, unitpri, amt, mem FROM ( SELECT *, @group_id := IF( @current_group = CONCAT_WS('|', vendorid, model, color), @group_id, @group_id + 1 ) AS group_id, @row_num := IF( @current_group = CONCAT_WS('|', vendorid, model, color), @row_num + 1, 1 ) AS row_num, @current_group := CONCAT_WS('|', vendorid, model, color) AS current_group FROM ( SELECT subptin1.invono, subptin1.date, subptin1.vendorid, subvendor1.vendor, subptou2.model, subptou2.color, subptin2.qty, subptin2.weight, subptin2.unitpri, ROUND(subptin2.qty * subptin2.unitpri, 2) AS amt, subptin2.mem FROM subptin1 INNER JOIN subptin2 ON subptin2.invono = subptin1.invono INNER JOIN subptou2 ON subptou2.mid = subptin2.id INNER JOIN subptou1 ON subptou1.invono = subptou2.invono LEFT JOIN subvendor1 ON subvendor1.vendorid = subptin1.vendorid WHERE DATE_FORMAT(subptin1.date, '%Y-%m') = DATE_FORMAT(NOW(), '%Y-%m') AND (subptin1.vendorid, subptou2.model, subptou2.color) IN ( SELECT v1.vendorid, m1.model, m1.color FROM subptin1 v1 INNER JOIN subptin2 d1 ON d1.invono = v1.invono INNER JOIN subptou2 m1 ON m1.mid = d1.id WHERE DATE_FORMAT(v1.date, '%Y-%m') = DATE_FORMAT(NOW(), '%Y-%m') GROUP BY v1.vendorid, m1.model, m1.color HAVING COUNT(DISTINCT d1.unitpri) > 1 ) ORDER BY subptin1.vendorid, subptou2.model, subptou2.color ) base CROSS JOIN (SELECT @group_id := 0, @row_num := 0, @current_group := '') vars ) numbered UNION ALL -- 添加空白行分隔每组 SELECT NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL FROM ( SELECT group_id, MAX(row_num) AS max_row FROM ( SELECT @group_id2 := IF( @current_group2 = CONCAT_WS('|', vendorid, model, color), @group_id2, @group_id2 + 1 ) AS group_id, @row_num2 := IF( @current_group2 = CONCAT_WS('|', vendorid, model, color), @row_num2 + 1, 1 ) AS row_num, @current_group2 := CONCAT_WS('|', vendorid, model, color) AS current_group FROM ( SELECT subptin1.vendorid, subptou2.model, subptou2.color FROM subptin1 INNER JOIN subptin2 ON subptin2.invono = subptin1.invono INNER JOIN subptou2 ON subptou2.mid = subptin2.id WHERE DATE_FORMAT(subptin1.date, '%Y-%m') = DATE_FORMAT(NOW(), '%Y-%m') GROUP BY subptin1.vendorid, subptou2.model, subptou2.color HAVING COUNT(DISTINCT subptin2.unitpri) > 1 ) base_groups CROSS JOIN (SELECT @group_id2 := 0, @row_num2 := 0, @current_group2 := '') vars ) tmp GROUP BY group_id ) group_max ORDER BY COALESCE(group_id, 999999), COALESCE(row_num, 999999); 这段代码,-- 添加空白行分隔每组 不正确,其他都是正确的,请修正
最新发布
08-20
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值