Mysql 按组获取百分比选择查询以在PHP SQL行中工作
我最终将此代码作为一个查询在MySQL中运行,但不是将两个SELECT查询合并在一行$sql PHP代码中:Mysql 按组获取百分比选择查询以在PHP SQL行中工作,mysql,Mysql,我最终将此代码作为一个查询在MySQL中运行,但不是将两个SELECT查询合并在一行$sql PHP代码中: SELECT count(*) from mytable into @AgentCount; SELECT area , COUNT( area ) AS thecount , ROUND (( COUNT( * ) / ( @AgentCount) * 100 ), 1 ) AS percentage FROM mytable GROUP BY area OR
SELECT count(*) from mytable into @AgentCount;
SELECT area
, COUNT( area ) AS thecount
, ROUND (( COUNT( * ) / ( @AgentCount) * 100 ), 1 ) AS percentage
FROM mytable
GROUP BY area
ORDER BY thecount DESC LIMIT 50;
同样,我们也不知道如何使用%符号进行取整,以使百分比达到53.3%(等等)。我比刚开始的时候更接近了,但还是没有雪茄
我很可能错过了一些简单的事情,但我的思想已经冻结了
谢谢你的除霜帮助
-stucko使用concat函数添加%符号并使用交叉连接
SELECT area
, COUNT( area ) AS thecount
, concat(ROUND((COUNT(*) / AgentCount * 100 ), 1), '%') AS percentage
FROM mytable
cross join
(SELECT count(*) AgentCount from mytable) t
GROUP BY area
ORDER BY thecount DESC LIMIT 50;
成功了,谢谢你!你们在堆栈溢出方面真的很快,也很有帮助。非常感谢。现在,要想知道如何颠倒PHP输出的顺序以显示%AREA,stuckoJust就知道了-谢谢你的贡献,海鹰-斯图克
SELECT area
, COUNT( area ) AS thecount
, concat(ROUND (( COUNT( * ) / ( total_count) * 100 ), 1 ),"%") AS percentage
FROM mytable,(SELECT count(*) as total_count from mytable)temp
GROUP BY area
ORDER BY thecount DESC LIMIT 50;