开发者

Get the MySQL record that has less child-records

I have a 2 MySQL tables like that:

Table: WIFI_Spots
*********************
ID  |  Name
1   |  Foo
2   |  Bar

Table: WIFI_Users
******开发者_如何学Python***************
Spot_ID  |  User_ID  |  Status
1        |  3h8n26j  |  active
1        |  h6m78v2  |  inactive
2        |  3v9bn4y  |  active
2        |  6f9ftfx  |  active

In that case i want to get the WIFI spot that has the less active users in it based on status.

So even if there was many inactive users, those wouldn't be counted.

P.S. In the example, the result would be the Spot 1.


SELECT    COUNT(s.ID)
FROM      WIFI_Spots s
LEFT JOIN WIFI_Users u ON u.Spot_ID = s.ID
WHERE     u.Status = 'active'
GROUP     BY s.ID
ORDER     BY COUNT(u.Spot_ID)
LIMIT     1
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜