在 MySQL 中为每个组生成序列 [英] Generating Sequence for Each Group in MySQL
本文介绍了在 MySQL 中为每个组生成序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何为 MySQL 生成这个序列?
(注意:-对于每个唯一的对,序列都会重新生成)
How to generate this sequence for MySQL?
(N.B. :- for each unique pair the sequence is generated afresh)
1,1 -- 1
1,1 -- 2
1,1 -- 3
1,2 -- 1
1,2 -- 2
1,3 -- 1
1,4 -- 1
1,4 -- 2
1,4 -- 3
1,1 -- 1
1,1 -- 2
1,1 -- 3
1,2 -- 1
1,2 -- 2
1,3 -- 1
1,4 -- 1
1,4 -- 2
1,4 -- 3
推荐答案
您需要 SQL Server for MySQL 中的 row_number
函数
You need the row_number
function in SQL Server for MySQL
SELECT
@row_number := CASE
WHEN @conc_no = conc THEN
@row_number + 1
ELSE
1
END AS num,
@conc_no := mydata.conc AS conc
FROM
(
SELECT
CONCAT(num1, ",", num2) AS conc
FROM
table1
ORDER BY
num1 ASC, num2 ASC
) mydata
小提琴:http://sqlfiddle.com/#!9/ad4a0/3/0
这篇关于在 MySQL 中为每个组生成序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文