我有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中哪个查询可以做到这一点?
谢谢你:)答案 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
答案 2 :(得分:1)
尝试这个..
select ID,Name,age,sequence_No
from Student having count(Name) > 1
order by Name asc