0

嗨,我有一个查询可以产生我需要的内容,但是我在输出中得到重复的行并且无法弄清楚为什么每行出现两次。有任何想法吗?

$query = "SELECT * FROM orders LEFT JOIN users ON orders.USER_ID = users.USER_ID
          LEFT JOIN items ON items.CHECKOUT_ID = orders.CHECKOUT_ID ORDER BY
           date_order DESC LIMIT 0,1000";



            $result = mysqli_query($con, $query);

            while($row = mysqli_fetch_array($result, MYSQL_ASSOC)){
                $order_id = $row["ORDER_ID"];
                $date_order = $row["date_order"];
                $date_req = $row["delivery"];
                $country = $row["country"];
                $firstname = $row["name"];
                $lastname = $row["surname"];
                $email = $row["email"];
                $size = $row["size"];
                $total_cost = $row["total_cost"];


                echo '<tr><td>' . $order_id . ' </td>' .
                     '<td>' . $date_order . '</td>' . 
                     '<td>' . $date_req . '</td>' . 
                     '<td>' . $country . ' </td>' . 
                     '<td>' . $firstname . ' </td>' .
                     '<td>' . $lastname . ' </td>' .
                     '<td>' . $email . ' </td>' .
                     '<td>' . $size . ' </td>' .
                     '<td>&euro;' . number_format($total_cost, 2, '.', '') . ' </td>' .
                     '<td style="text-align:right"><a href="xxxxxxx_Order_Details_Admin.php?id=' . $order_id . '">More Details</td>' .
                     '<td style="text-align:right"><a href="xxxxxxxx_Order_Details_print.php?id=' . $order_id . '">Print</td>' .

                    '</tr>';             
4

2 回答 2

1

您的查询应该为行中的每个项目返回一行。这是你所期待的吗?如果您期望每个用户一行或每个订单一行,那么您的查询错误。

从您提取的字段中,我认为您不想要这些项目。试试这个查询:

SELECT *
FROM orders LEFT JOIN
     user
    ON orders.USER_ID = users.USER_ID
ORDER BY date_order DESC LIMIT 0,1000

如果您仍然得到重复项(或者当您期望每个项目一行时重复行),那么您需要查看基础表以查看重复项的来源。

于 2012-08-28T15:41:37.447 回答
0

将您的查询更改为:

SELECT DISTINCT o.ORDER _ID, o.date_order, ...
FROM orders o
  LEFT JOIN users u
    ON o.USER_ID = u.USER_ID 
  LEFT JOIN items i
    ON i.CHECKOUT_ID = o.CHECKOUT_ID 
ORDER BY date_order DESC LIMIT 0,1000

哦,始终指定要返回的列。使用*使调试变得更加困难。

于 2012-08-28T11:54:13.107 回答