【问题标题】:Count variables with same name and display name and number计算具有相同名称和显示名称和编号的变量
【发布时间】:2012-06-18 18:17:03
【问题描述】:

我有下一个 mysql 查询:

    $productn = $jdb->query('SELECT t2.title FROM '.DB_PREFIX.'shopping_cart AS t1 LEFT JOIN '.DB_PREFIX.'shop_order_details AS t2 ON t1.shopid WHERE t1.session = "'.smartsql($_SESSION['shopping_cart']).'"');

while ($rowp = $productn->fetch_assoc()) {
// my product title
 $productname = $rowp["title"];
}

使用 print_r($productname);我得到了类似的东西:

姓名1姓名1姓名1姓名2姓名2

我想显示如下内容:

3 x 名称1 2 x 名称2

3 x 名称1,2 x 名称2

这个可以吗?

【问题讨论】:

标签: php mysql function printing count


【解决方案1】:

SQL 查询是这样的:

select name,count(name) from table group by name ;

希望对你有所帮助。

【讨论】:

    【解决方案2】:

    如果你想用 PHP 来做,请使用:

    $total_products = array();
    while ($rowp = $productn->fetch_assoc()) {
       $productname = $rowp['title'];
       if(isset($total_products[$productname])) {
          ++$total_products[$productname];
       }
       else
       {
          $total_products[$productname] = 1;
       }
    }
    

    UPD 1: 以所需格式显示产品:

    foreach($total_products as $name => $num) {
       echo "{$num} x {$name}, ";
    }
    

    【讨论】:

    • 不,我得到的只是“数组([Name1] => 36 [Name1] => 162 [Name1] => 219)数组([Name2] => 36 [Name2] => 165 [Name1] => 219) 数组 ([Name2] => 36 [Name2] => 165 [Name1] => 222) "。我有 2 x Name1 和 1 x Name2。谢谢顺便说一句
    • @demlasjr 您查看它是因为您需要使用 foreach 循环以所需格式显示数据。看看我的更新。
    • 它只是从我的数据库中显示不同的订单,没有任何相关性。我认为是阵列故障。
    【解决方案3】:

    您可以通过这样的计数(*)分组功能使用分组关闭

    $productn = $jdb->query('SELECT t2.title, count(t2.title) num FROM '.DB_PREFIX.'shopping_cart AS t1 LEFT JOIN '.DB_PREFIX.'shop_order_details AS t2 ON t1.shopid WHERE t1.session = "'.smartsql($_SESSION['shopping_cart']).'" group by t2.title');
    
    while ($rowp = $productn->fetch_assoc()) {
    // my product title
     $productname = $rowp["title"];
     $numberofproducts  = $rowp["num"];
    }
    

    这应该可以...

    【讨论】:

    • 得到类似 Namex210Namex210Namex210 where x=1 的东西(通过这种方式知道错误在哪里)。我有 2xName1 和 1xName2。感谢您的帮助
    • 将 count(*) 更改为 count(t2.title)。 210大概是总行数
    【解决方案4】:

    好的,我终于可以做出我想要的了,所以我想在这里分享它以供将来使用。

    这就是我所做的:

    $productn = $jdb->query('SELECT t2.title, count(t2.title) num FROM '.DB_PREFIX.'shop_order_details AS t2 WHERE t2.orderid ="'.$orderid.'" GROUP BY t2.title');
    
    while($registro=$productn->fetch_array()){
     echo $registro['title'].' x '.$registro['num']."<br>";
    }
    

    我在课堂上定义了 fetch_array():

        function fetch_assoc() {
        if($this->result) {
            return mysql_fetch_assoc($this->result);
        } else {
            return false;
        }
    }
    

    感谢大家的帮助。我给大家加分了!

    【讨论】:

      【解决方案5】:

      您可能希望按标题分组并计算每个分组的出现次数。

      SELECT t2.title, COUNT(t2.title)
      FROM shopping_cart AS t1
      LEFT JOIN shop_order_details AS t2 ON t1.shopid
      WHERE t1.session = <session>
      GROUP BY t2.title
      

      查看COUNT(*)GROUP BY 函数参考。

      【讨论】:

        猜你喜欢
        • 2013-06-16
        • 1970-01-01
        • 2011-04-14
        • 2021-05-01
        • 2020-11-03
        • 2012-08-03
        • 2023-03-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多