Database

[mysql] SQL order string as number

컨텐츠 정보

본문

I have numbers saved as VARCHAR to a MySQL database. I can not make them INT due to some other depending circumstances.

It is taking them as character not as number while sorting.

In database I have

1 2 3 4 5 6 7 8 9 10...

On my page it shows ordered list like this:

1 10 2 3 4 5 6 7 8 9

How can I make it appear ordered by numbers ascending?



---------------------------------------------------------------------------------


select col from yourtable
order by cast(col as unsigned)

or to force a conversion to number do:

select col from yourtable
order by col * 1

관련자료

댓글 0
등록된 댓글이 없습니다.
Today's proverb
현명해지는 길이요? 그거요. 간단한 일이죠. 실수하고 또 실수하고 다시 또 실수하더라도, 조금만 덜, 다시 조금만 덜, 다시 또 조금만 덜 실수해 나가면 되는 거죠. <파이어트 하인의 (Grooks 1) 중에서>