简体   繁体   English

在php mysql中与group by一起计数

[英]Count with group by in php mysql

Product Table : will display all product - list产品表:将显示所有产品 - 列表

prodid |  name   | size
1      |  prd1   |  30
2      |  prd2   |  40
3      |  prd3   |  50
4      |  prod4  |  20

Product Details: - when product list is display , i have text box for each product, where resp.产品详细信息: - 显示产品列表时,我为每个产品都有文本框,其中 resp。 user fill value(500,300..etc) and submit data ..this data is store in product details as below用户填写值(500,300..etc)并提交数据..此数据存储在产品详细信息中,如下所示

id | prdid | userid | value |
1  |  1    |  2     |  500
2  |  2    |  2     |  200 
3  |  1    |  3     |  230 
4  |  3    |  2     |  300
5  |  1    |  4     |  100

What i want :我想要的是 :

Product Count  |  Product Name  | Product Size 
   3           |    prod1       |   30
   1           |    prod2       |   40
   1           |    prod3       |   50
               |    prod4       |   20

below is list of table :以下是表格列表:

    $sql = "SELECT * FROM product ";
    $result = $conn->query($sql);

    echo "<table border='1'>
      <tr>
         <th>Product Count</th>
         <th>Product Name</th>
         <th>Product Size</th>
      </tr>";

   while($row = mysqli_fetch_array($result)) {
      echo "<tr>";
      echo "<td>" . ???? . "</td>";
      echo "<td>" . $row['name'] . "</td>";
      echo "<td>" . $row['size'] . "</td>";
      echo "</tr>";
   }
   echo "</table>";

from above i need 2 things: 
1 } product count - count is common 
2 } highlight with red/green that count for resp user who's value is higher for that prdid from product details

ex : in product details :例如:在产品详细信息中:

prdid 1 has 3 count prdid 1 有 3 个计数

user for prdid 1 are 3 ( 2,3,4) out of which userid 2 has higher value which is 500 hence only his product column for that prodid will he green else red prdid 1 的用户是 3 ( 2,3,4),其中 userid 2 具有更高的值,即 500,因此只有他的那个 prodid 的产品列才会变绿,否则变红

hope you guys have understood my requirement....希望你们已经明白我的要求......

Hi since your select columns were not in group by clause I think you might have faced some problems.您好,因为您的选择列不在 group by 子句中,我认为您可能遇到了一些问题。 And you have to do left join so that you can get the product details of prd4 which is having count 0 else you wont get that record.并且您必须进行左连接,以便您可以获得计数为0prd4的产品详细信息,否则您将无法获得该记录。

The following query works fine :以下查询工作正常:

SELECT count(pd.prdid) AS productCount, p.name, p.size 
FROM `product` AS p 
LEFT JOIN product_details AS pd ON p.prodid = pd.prdid
GROUP BY p.name,p.size

也许这个查询:

SELECT COUNT(details.*) AS NbProduct, product.name, product.size FROM details JOIN product ON product.id = details.product_id GROUP BY product.name

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM