Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何替换sql版本4.9.5中的Over子句_Php_Sql_Database - Fatal编程技术网

Php 如何替换sql版本4.9.5中的Over子句

Php 如何替换sql版本4.9.5中的Over子句,php,sql,database,Php,Sql,Database,我正在尝试运行一个查询,以查找基于性别的artisan的计数和百分比。查询在SQL server 8上运行得非常好。但不是在我的live server 4.9.5上。 以下是我的疑问 SELECT industry , count(*) as cnt , (count(*)*100.0/sum(count(*))over()) as perc FROM `artisan_work` GROUP BY industry ORDER BY cnt DESC 在任

我正在尝试运行一个查询,以查找基于性别的artisan的计数和百分比。查询在SQL server 8上运行得非常好。但不是在我的live server 4.9.5上。 以下是我的疑问

SELECT industry
       , count(*) as cnt
       , (count(*)*100.0/sum(count(*))over()) as perc 
FROM `artisan_work` 
GROUP BY industry 
ORDER BY cnt DESC 

在任何数据库中,您都应该能够使用:

SELECT aw.industry, count(*) as cnt,
       count(*) * 100.0 / tot.cnt as perc 
FROM artisan_work aw cross join
     (SELECT COUNT(*) as cnt FROM artisan_work) tot
GROUP BY aw.industry 
ORDER BY cnt DESC 

什么数据库?什么的4.9.5?这对任何数据库来说都不重要。请分享更多详细信息。给定的问题与PHP有什么关系?我通过这个查询找到了用户的百分比,然后在HTML表中以百分比的形式显示perc。请通过编辑它来为您的问题添加所有澄清。谢谢,它很有效。整个查询工作正常,但百分比显示为100%,所有情况下,您能帮助我吗that@HusnainYusufi . . . 你能在DB FIDLE中提供一些示例数据吗?下面是查询代码,希望你得到这个$ret=mysqli\u query$con,选择行业,计数为cnt,选择artisan\u work b中的计数,其中a.industry=b.industry*100.0/count*as perc from artisan\u work a GROUP BY industry ORDER BY cnt DESC;$cnt=1;$sum=0;而$row=mysqli\u fetch\u array$ret{?>。%现在,这个查询显示每个细节都是100%。你能解决这个问题吗?谢谢,它很有效。非常欢迎。祝你一切顺利。@Husnainyusfi…我认为这与你问题中的查询不同。整个查询工作正常,但在所有情况下百分比都是100%I’’很抱歉,我已经修改了我的答案,现在应该可以了。
SELECT industry
       , count(*) as cnt
       , (count(*)*100.0/(select count(*) from artisan_work )) as perc 
FROM artisan_work  a
GROUP BY industry 
ORDER BY cnt DESC