在mysql中编号重复记录

时间:2013-07-08 07:57:19

标签: mysql sql

我有Student表,我想更新(sequence_No。)字段:

ID        Name      age     sequence_No.
--        -----     ---     ------------

1         sara      20         1

2         sara      20         2

3         sara      20         3

4         john      24         1

5         john      24         2

6         Hama      23         1

在mysql中哪个查询可以做到这一点?

谢谢你:)

3 个答案:

答案 0 :(得分:3)

SELECT 
ID,
Name,
age,
( 
CASE Name 
WHEN @curType 
THEN @curRow := @curRow + 1 
ELSE @curRow := 1 AND @curType := Name END
) + 1 AS sequence_No

FROM student, (SELECT @curRow := 0, @curType := '') r
ORDER BY  ID,NAME;

<强> SQL FIDDLE

答案 1 :(得分:3)

希望这会对你有所帮助:

SELECT *, count(*) as seq_number FROM student a
JOIN student b ON a.name = b.name AND a.id >= b.id
GROUP BY a.id

Sqlfiddle

答案 2 :(得分:1)

尝试这个..

select ID,Name,age,sequence_No 
from Student having count(Name) > 1 
order by Name asc