dqc3469 2012-02-22 14:15
浏览 20
已采纳

Mysql重复结果

I'm making a invoice script but my query is returning al my entries mulpiple times when i fetch them.

this is my code:

$query      =   "   SELECT d.Quantity, d.ProductID, p.ProductName, d.UnitPrice, d.Discount
                            FROM customers AS c, orders AS o, order_details AS d, products AS p
                            WHERE o.OrderID = '10248'
                            AND o.OrderID = d.OrderID
                            AND d.ProductID = p.ProductID
                        ";

        $result     =   mysql_query($query);

        $table      =   '';

        while($row  =   mysql_fetch_assoc($result)){
            $table  .=  '<tr>';
            $table  .=  '<td>' . $row['Quantity'] . '</td>';
            $table  .=  '<td>' . $row['ProductID'] . '</td>';
            $table  .=  '<td>' . $row['ProductName'] . '</td>';
            $table  .=  '<td>' . $row['UnitPrice'] . '</td>';
            $table  .=  '<td>' . $row['Discount'] . '</td>';
            $table  .=  '<td>' . (100 - $row['Discount']) / 100 * $row['UnitPrice'] . '</td>';
            $table  .=  '</tr>';
        }

this is, a part, of what it returns.

Quantity    ProductID   ProductName     UnitPrice   Discount    Subtotal
12  11  Queso Cabrales  14.0000 0   14
10  42  Singaporean Hokkien Fried Mee   9.8000  0   9.8
5   72  Mozzarella di Giovanni  34.8000 0   34.8
12  11  Queso Cabrales  14.0000 0   14
10  42  Singaporean Hokkien Fried Mee   9.8000  0   9.8
5   72  Mozzarella di Giovanni  34.8000 0   34.8
12  11  Queso Cabrales  14.0000 0   14
10  42  Singaporean Hokkien Fried Mee   9.8000  0   9.8
5   72  Mozzarella di Giovanni  34.8000 0   34.8
12  11  Queso Cabrales  14.0000 0   14
10  42  Singaporean Hokkien Fried Mee   9.8000  0   9.8
5   72  Mozzarella di Giovanni  34.8000 0   34.8

while it sould only return 3 entries.

anny toughts?

  • 写回答

3条回答 默认 最新

  • dougu9895 2012-02-22 14:20
    关注

    You have a Cartesian join:

    SELECT d.Quantity, 
           d.ProductID, 
           p.ProductName, 
           d.UnitPrice, 
           d.Discount
    FROM   **customers AS c,** 
           orders AS o, 
           order_details AS d, 
           products AS p
    WHERE  o.OrderID = '10248'
    AND    o.OrderID = d.OrderID
    AND    d.ProductID = p.ProductID
    

    You need to include a join to customers, or drop that table from the SQL.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 基于52单片机的酒精浓度检测系统加继电器和sim800
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答