Php 每行有两个互补值的查询
我在一个非常大的查询中有一个子查询,它必须执行以下操作 有一对簇的数组 数组(数组(c1,c2),数组(c3,c4),数组(c5,c6),数组(c7,c8)) 其中,例如c1和c2是互补的,c3和c4也是互补的。 我有一个表格状态:Php 每行有两个互补值的查询,php,mysql,Php,Mysql,我在一个非常大的查询中有一个子查询,它必须执行以下操作 有一对簇的数组 数组(数组(c1,c2),数组(c3,c4),数组(c5,c6),数组(c7,c8)) 其中,例如c1和c2是互补的,c3和c4也是互补的。 我有一个表格状态: id_state cluster successfull failed success_ratio 1 c1 4 0 100% 2 c2
id_state cluster successfull failed success_ratio
1 c1 4 0 100%
2 c2 1 9 10%
3 c3 0 4 0%
4 c4 1 1 50%
注意,哪个集群和另一个集群耦合是使用上面的数组确定的
以及我想要的最终输出:
cluster successfull success_ratio
c1 4 100% (for the first pair)
c4 1 50% (for the second)
有没有一种方法可以通过只获取数据来查询所有数据的成功率
从每对夫妇中选择成功率>50%的一对,只有当两人的成功率都<50%时,才选择第一对
仅使用mysql查询就可以实现这一点吗(我不能使用查询结果,因为我希望它作为另一个大型查询的子查询)
即使你能建议一个起点,你也会很感激的。听起来你只是想要每一对的最大成功率
select s.grp, max(success_ratio)
from state s join
(select 'c1' as cluster, 1 as grp union all
select 'c2', 1 union all
select 'c3', 2 union all
select 'c4', 2 union all
select 'c5', 3 union all
select 'c6', 3 union all
select 'c7', 4 union all
select 'c8', 4
) grps
on s.cluster = grps.cluster
group by s.grp;
如果您确实希望获得最成功的行,请使用子查询:
select s.*
from (select s.grp,
substring_index(group_concat(cluster order by success_ratio desc), ',', 1) as bestcluster
from state s join
(select 'c1' as cluster, 1 as grp union all
select 'c2', 1 union all
select 'c3', 2 union all
select 'c4', 2 union all
select 'c5', 3 union all
select 'c6', 3 union all
select 'c7', 4 union all
select 'c8', 4
) grps
on s.cluster = grps.cluster
group by s.grp
) b join
state s
on s.cluster = b.cluster
如果两者的成功率都超过50%,你能举一个你想要的输入和输出的例子吗?@gordon linoff,那么我就拿第一个成功为例ratio@alex-w刚刚为itthanks@gordon linoff添加了一个编辑,这是一个非常有用的答案,为我指明了正确的方向