study/mysql
mysql select 할 때 순환자 넘버링하기 (iterator numbering)
TY
2013. 2. 20. 01:02
table test 의 column이
name, score가 있다고 할 때
John / 50
Matthew / 40
Cain / 70
일 때
select @rownum:=@rownum+1 as num, t.* from test t, (SELECT @rownum:=0) r
로 하면
num/name/score
1 / John / 50
2 / Matthew / 40
3 / Cain / 70
로 되고
select @rownum:=@rownum+1 as num, t.* from test t, (SELECT @rownum:=0) r ORDER BY score DESC
로 하면
1 / Cain / 70
2 / John / 50
3 / Metthew / 40
로 나온다