I am looking for a way to select a single row from a My开发者_Python百科SQL table. One of the columns contains a hexidecimal value and I only want to SELECT the row which contains the largest hex value.
So for instance there are three rows like:
id hex_value comments
1 01 some comments...
2 1A some comments...
3 03 some comments...
So the resultant returned rows contents would be id 2 as the value 1A is the greatest.
If anyone could point me in the right direction it would be most appreciated.
Thank you Mark
select ...
order by CONV(hex_value,16,10)
limit 1
see http://dev.mysql.com/doc/refman/5.0/en/mathematical-functions.html#function_conv
SELECT ... ORDER BY UNHEX(hex_value) desc LIMIT 1
should work
As long as the hexadecimal strings are the same length (as in your example), they are comparable as strings, so you can just sort them:
select id, hex_value, comments
from TheTable
order by hex_value desc
limit 1
Note: You need to use desc
when sorting to get the highest value first.
精彩评论