2

我有学生表,我想像这样更新 (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中做到这一点?

谢谢你 :)

4

3 回答 3

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 小提琴

于 2013-07-08T08:10:58.157 回答
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

于 2013-07-08T08:12:48.700 回答
1

试试这个..

select ID,Name,age,sequence_No 
from Student having count(Name) > 1 
order by Name asc
于 2013-07-08T08:02:22.407 回答