MYSQL: Get row where one specific property is smaller than a few other
I'd like to know how to do this kind of query:
I have one table with a bunch of properties. In these properties, there are 'distanceSydney, distanceBrisbane, distanceCanberra开发者_StackOverflow' and so on..
I'd like to count the number of rows where for example, distanceSydney is the smallest. I think I need a MIN() function that would return the smallest of the args we give it. I couldn't find this function...
I tried:
SELECT COUNT(id) FROM `shops` WHERE MIN( `distanceSydney`, `distanceCanberra` ) = `distanceSydney`
But of course, MySQL error.
Thanks in advance! Bastien.
Won't this do?
SELECT COUNT(id) FROM shops WHERE distanceSydney <= distanceCanberra
SELECT COUNT(*) as count
FROM shops
WHERE distanceSydney < distanceCanberra
AND distanceSydney < distanceBrisbane
AND distanceSydney < distanceMelbourne
-- etc for other distance columns
The MIN
function exists, you just have to use it a little differently. Get the minimal value first, and then COUNT
the rows WHERE
it's that value
精彩评论