嗨我有一个产生我需要的查询,但是我在输出上得到重复的行并且无法找出原因,每行出现两次。有什么想法吗?
$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>€' . 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>';
答案 0 :(得分:1)
您的查询应该为该行中的每个项目返回一行。这是你期待的吗?如果您希望每个用户一行或每个订单一行,那么您的查询错误。
从您要拔出的字段中,我认为您不想要这些项目。请尝试此查询:
SELECT *
FROM orders LEFT JOIN
user
ON orders.USER_ID = users.USER_ID
ORDER BY date_order DESC LIMIT 0,1000
如果您仍然在获得重复项(或者当您希望每个项目有一行时重复行),那么您需要查看基础表以查看重复项的来源。
答案 1 :(得分: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
哦,总是指定要返回的列。使用*
会使调试变得更加困难。