PHP: echo number of duplicates in a table by most common

后端 未结 4 1503
臣服心动
臣服心动 2021-01-20 02:16

I am trying to echo the most common duplicates in a table. I have been able to echo the name of the duplicates but what I\'m looking for is a separate column in the table th

相关标签:
4条回答
  • 2021-01-20 02:30

    you need to use an alias for count(*).Then you can use it for order and echo.

    $interests = "SELECT name, COUNT(*) as count_alias FROM pageinterests WHERE pageid = '$id' GROUP BY name HAVING COUNT(*) > 1 ORDER BY count_alias DESC";
    
    0 讨论(0)
  • 2021-01-20 02:35

    Use an alias and order by for correct count sequence

        $interests = "SELECT name, COUNT(*) AS my_count 
              FROM pageinterests 
              WHERE pageid = '$id' 
              GROUP BY name HAVING COUNT(*) > 1
              ORDER BY my_count";
    
    
        .......
    
       for($x = 1; $x <= $rim; $x++){
           $rat = mysqli_fetch_assoc($interestresults);
           echo "<tr>
    
            <td>".$rat['name']."</td>
            <td>".$rat['my_count']."</td>
              </tr>";
    
        }
    
    0 讨论(0)
  • 2021-01-20 02:38

    You could use an alias like this:

    SELECT name, COUNT() as The_Count 
    FROM pageinterests
    WHERE pageid = '$id'
    GROUP BY name HAVING COUNT() > 1
    ORDER BY The_Count DESC;
    
    0 讨论(0)
  • 2021-01-20 02:43
    SELECT name, COUNT(*) as mycount FROM ...
    
    0 讨论(0)
提交回复
热议问题