我有两个表ss_test和ss_ceastore_config ss_test有一个名为store_id的字段,它映射到ss_ceastore_config id。我的ss_test包含服务器条目,它告诉它正在使用的是哪个存储库,所以我试图找到哪个存储区被使用了min时间和它的id。我已经写了下面的查询。
select id,
min(server_counts) as server_counts,
isalive
from (select ss_ceastore_config.id ,
count(server_id)as server_counts,
ss_ceastore_config.isalive
from ss_test
right join ss_ceastore_config
on ss_test.store_id = ss_ceastore_config.id
group by store_id
order by id) join_1我的内部查询给出了正确的结果如下
id Ascending server_counts isalive
1 5 1
2 0 1因此,我希望使用min函数从内部查询输出中选择下面的记录
id Ascending server_counts isalive
2 0 1但它提供了意外的结果,我的外部查询如下
id server_counts isalive
1 0 1为什么会这样?为什么要为server_counts 0提供id 1?如何修复此查询?
发布于 2015-05-28 05:36:22
select id,
server_counts,
isalive
from (select ss_ceastore_config.id ,
count(server_id)as server_counts,
ss_ceastore_config.isalive
from ss_test
right join ss_ceastore_config
on ss_test.store_id = ss_ceastore_config.id
group by store_id
order by id) join_1
order by server_counts asc
limit 1;您的原始查询无法工作,因为您正在使用SELECT函数以及非聚合列(如id和isalive )来执行聚合查询。我相信MySQL不能保证它将返回哪个id值以及该列的最小值。
我的策略是按server_counts的升序返回所有行,然后只返回第一行(这是最小行)。
https://stackoverflow.com/questions/30497921
复制相似问题