开发者

MYSQL: Get row where one specific property is smaller than a few other

开发者 https://www.devze.com 2023-03-26 14:43 出处:网络
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\'

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

0

精彩评论

暂无评论...
验证码 换一张
取 消