gpt4 book ai didi

php - 数据库最常见条目查询

转载 作者:行者123 更新时间:2023-12-01 00:50:15 24 4
gpt4 key购买 nike

有帮助显示结果的帮助,它没有显示我需要的结果。

我有这个

表名=幂列名 = power1, power2, power2, power4, power5

 $res = mysql_query("select power1, power2, power2, power4, power5, 
count(*) as cnt
from power
group by power1, power2, power2, power4, power5
order by cnt desc limit 5;");

它显示了表格中的特定行,而不仅仅是表格中最常见的 5 个数字

the table entries

the results

如您所见,它没有显示前 5 个数字(至少应显示数字 1,5 和 2),而是显示第 2 行。

任何想法都会受到热烈欢迎。

非常感谢

保罗

最佳答案

获取表 xcol 列中最常见的 5 个值:

select col, count(*) as cnt
from x
group by col
order by cnt desc
limit 5;

如果您想要获得五列中最常见的五个数字(在我的原始答案之后对问题进行了主要更改),这是一种方法:

select power, count(*) as cnt
from (select (case when n = 1 then power1
when n = 2 then power2
when n = 3 then power3
when n = 4 then power4
when n = 5 then power5
end) as power
from power p cross join
(select 1 as n union all select 2 union all select 3 union all select 4 union all select 5
) n
) p

这会将所有列放入一个列中,然后返回最常见的值。

关于php - 数据库最常见条目查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16652219/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com