最大计数查询

时间:2015-09-06 22:11:02

标签: mysql sql qsqlquery

这是我的表,表名是“ad_publisher_details”。我想找到最大的发布商公司名称是指拥有最多行数的公司名称。有人可以帮忙吗?

表: -

<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>

在SQL查询之后,它将仅返回publisher_company值“Microsoft Bangladesh”。因为它排得最多。

2 个答案:

答案 0 :(得分:1)

你应该:

  1. GROUP BY发布商
  2. ORDER BY行数,按降序排列。
  3. LIMIT结果为单行。
  4. 换句话说,你可以这样做:

    SELECT publisher_company
     FROM ad_publisher_details
     GROUP BY publisher_company
     ORDER BY COUNT(*) DESC
     LIMIT 1;
    

答案 1 :(得分:0)

最后我找到了解决方案。我必须做更多的PHP代码才能这样做。这是我的解决方案。

<?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;
                                                }
                                            }

                                            ?>