[英]Max Count Query
This is my table and the table name is "ad_publisher_details".这是我的表,表名是“ad_publisher_details”。 I want to find the maximum publisher company name means the company name who has most number of rows.
我想找到最大的出版商公司名称意味着拥有最多行数的公司名称。 Can anyone help?
任何人都可以帮忙吗?
Table:-桌子:-
<table> <thead> <th> ad_id </th> <th> publisher_name </th> <th> publisher_company </th> </thead> <tbody> <tr> <td> 1 </td> <td> bikroy manager </td> <td> bikroy.com </td> </tr> <tr> <td> 2 </td> <td> olx manager </td> <td> olx.com </td> </tr> <tr> <td> 3 </td> <td> microsoft manager </td> <td> microsoft bangladesh </td> </tr> <tr> <td> 4 </td> <td> microsoft manager </td> <td> microsoft bangladesh </td> </tr> <tr> <td> 5 </td> <td> marketing manager </td> <td> land rover </td> </tr> </tbody> </table>
After SQL query it will return only the publisher_company value "Microsoft Bangladesh".在 SQL 查询之后,它将只返回publisher_company 值“Microsoft 孟加拉国”。 As it has the most row.
因为它有最多的行。
You should:你应该:
GROUP BY
publisher GROUP BY
出版商GROUP BY
ORDER BY
number of rows, in descending order. ORDER BY
行数,按降序排列。LIMIT
the results to single row.LIMIT
为单行。 In other words you can do:换句话说,你可以这样做:
SELECT publisher_company
FROM ad_publisher_details
GROUP BY publisher_company
ORDER BY COUNT(*) DESC
LIMIT 1;
And at last i found the solution.最后我找到了解决方案。 I have to do a lot more php code to do so.
我必须做更多的 php 代码才能做到这一点。 Here is my solution.
这是我的解决方案。
<?php
$i=0;
$j=0;
$br_temp=0;
$query1="SELECT publisher_company FROM ad_publisher_details WHERE 1";
$result1=mysql_query($query1,$dbs);
while($row1=mysql_fetch_array($result1))
{
$publisher_company[$i]=$row1['publisher_company'];
$i++;
}
for($i=0;$i<count($publisher_company);$i++)
{
$temp2= $publisher_company[$i];
$query2="SELECT COUNT(ad_id) AS count_ad FROM ad_publisher_details WHERE publisher_company='$temp2'";
$result2=mysql_query($query2,$dbs);
while($row2=mysql_fetch_array($result2))
{
$most_ad[$j]=$row2['count_ad'];
$j++;
}
}
$highest_ad = max($most_ad);
for($i=0;$i<count($publisher_company);$i++)
{
$i_temp = $publisher_company[$i];
$query3="SELECT COUNT(ad_id) AS match_ad FROM ad_publisher_details WHERE publisher_company='$i_temp'";
$result3=mysql_query($query3,$dbs);
while($row3=mysql_fetch_array($result3))
{
if($highest_ad==$row3['match_ad'])
{
echo $i_temp;
$br_temp=1;
break;
}
if($br_temp==1)
{
break;
}
}
if($br_temp==1)
{
break;
}
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.