【问题标题】:sql count only once per shop_id每个 shop_id 只计算一次 sql
【发布时间】:2013-11-23 03:51:44
【问题描述】:
$query = "SELECT shop_id, article_id, design_id FROM shop_tshirts WHERE design_id = '$design'";
$zres = mysql_query($query) or die(mysql_error());
$total = mysql_num_rows($zres);

我使用此代码获取使用特定 $design 的产品数量。

是否可以每个 shop_id 只计算一次产品?

此数据库结构的示例:

shop_id / article_id / design_id
S01 / A01 / D01
S02 / A02 / D01
S03 / A03 / D01
S03 / A04 / D01

通常使用这种结构,我的代码的结果将是 4...我想更改代码,因此结果将是 3,因为 D01 在 S03 中被使用了两次,我想每个商店只计算一次.

谢谢!

【问题讨论】:

  • 你可以使用 distinct...select distinct shop_id

标签: php mysql sql math count


【解决方案1】:
SELECT distinct shop_id, article_id, design_id FROM shop_tshirts WHERE design_id = '$design'

【讨论】:

    【解决方案2】:

    您必须使用 SQL SELECT DISTINCT 语句

    $query = "SELECT DISTINCT shop_id, article_id, design_id FROM shop_tshirts WHERE design_id = '$design'";
    

    【讨论】:

      【解决方案3】:

      您需要唯一的 shop_id 值

      您不需要 article_id 或 design_id

      $query = "SELECT DISTINCT shop_id FROM shop_tshirts WHERE design_id ='$design'";
      $zres = mysql_query($query) or die(mysql_error());
      $total = mysql_num_rows($zres);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-07-07
        • 2016-03-05
        • 1970-01-01
        • 1970-01-01
        • 2020-06-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多