douduoting8408 2018-08-15 22:24
浏览 32
已采纳

PHP + mySQL - 在计数查询后获取行的共享位置

So I'm trying to display individual ranks of each seller within their panel on my marketplace site. For this specific rank, I'm counting how many followers each seller has, ordering the list in descending order, and then getting the position of each one so they know where they rank. I'm using a query that works, except when two sellers have the same number of followers, the position changes every time. This is especially bothersome when there are large number of sellers with the same number of followers.

Query (in PHP):

<?php 
    $query = "SELECT rank
        FROM (
          SELECT vendor_id, followers, @rank:=@rank+1 AS rank
          FROM (
            SELECT vendor_id, COUNT(*) AS followers
            FROM $table
            GROUP BY vendor_id
            ORDER BY followers DESC
           ) as sq,
            (SELECT @rank:=0) AS tr
          ) as q WHERE vendor_id = $vendorId";
?>

where $vendorId = the current seller's ID.

So, again, the main issue is that if there are 10 sellers, and 7 of them all have 1 follower. Each time the seller refreshes, they'll see a ranking anywhere from 4-10. I'd like it to just say 4 for all sellers who have the 1 follower.

Update: The following query has given me what I wanted, though I'd still prefer pushing the workload to PHP than keeping it all on MySQL.

<?php
    $connection->query("SET @curRank:=1, @prevRank:=0, @incRank=1;");

    $query = "SELECT rank
    FROM (
    SELECT vendor_id, followers,
      @curRank := IF(@prevRank = followers, @curRank, @incRank) AS rank,
      @incRank := @incRank + 1,
      @prevRank := followers
        FROM (
    SELECT vendor_id, COUNT(1) AS followers
    FROM $favTable
            GROUP BY vendor_id
    ORDER BY followers DESC, vendor_id DESC
        ) as sq,
        (SELECT @rank:=0) AS tr
     ) as q WHERE vendor_id = $vendorId";

    $result = $connection->fetchOne($query);
  • 写回答

1条回答 默认 最新

  • duancheng3042 2018-08-15 23:11
    关注

    You are on the right path. You just need to add a couple more vars in the query. You could try the following:

    <?php 
    $query = "SELECT rank
        FROM (
          SELECT vendor_id, followers,
              @curRank := IF(@prevRank = followers, @curRank, @incRank) AS rank, 
              @incRank := @incRank + 1, 
              @prevRank := followers
          FROM (
            SELECT vendor_id, COUNT(*) AS followers
            FROM $table
            GROUP BY vendor_id
            ORDER BY followers DESC, vendor_id
           ) as sq,
            (SELECT @rank:=0) AS tr
          ) as q WHERE vendor_id = $vendorId";
    ?>
    

    Please add a more specific sort order as mentioned by @swa66 to get consistent ordered rows. When the field used in sort has same values, mysql may return rows based on its own ordering logic.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥15 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)