Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using CAST() with MAX()/MIN()

I have some street numbers stored as VARCHARs but I want to sort them where possible as INTs. This syntax is accepted but produces alpha sorted sets like max = 53, min = 1203

SELECT  `StreetName`, 
CAST( MIN( `StreetNumber` ) AS UNSIGNED)  as min,
CAST( MAX( `StreetNumber` ) AS UNSIGNED)  as max
FROM  `tblAddToLotBridge` 
WHERE  `LotNumber` = '$item_lotnum'
AND `Primary_Secondary` = 0 
GROUP BY `StreetName`
ORDER BY `StreetName`

I tried nesting CAST() inside of MIN()/MAX() but that syntax got rejected.

What is the correct sytax to DWIM?

like image 262
jerrygarciuh Avatar asked Mar 29 '11 19:03

jerrygarciuh


1 Answers

Is this what you tried?

SELECT  `StreetName`, 
MIN( CAST( `StreetNumber` AS UNSIGNED) ) as min,
MAX( CAST( `StreetNumber` AS UNSIGNED) ) as max
FROM  `tblAddToLotBridge` 
WHERE  `LotNumber` = '$item_lotnum'
AND `Primary_Secondary` = 0 
GROUP BY `StreetName`
ORDER BY `StreetName`
like image 115
RichardTheKiwi Avatar answered Oct 08 '22 11:10

RichardTheKiwi