如果所有行值都设置为相同的值,我正在尝试更新状态。
我这样的查询
$ord = "SELECT * FROM plitems WHERE pur_order_id='".$order_id."'";
$ord1 = mysqli_query($con, $ord);
while($ord2 = mysqli_fetch_array($ord1)) {
if(($ord2['invoice_status']=='Invoiced'))
{
$m5 = mysqli_query($con, "UPDATE pur_orders SET status='Invoiced' WHERE pur_order_id='".$order_id."'");
}
else
{
$m5 = mysqli_query($con, "UPDATE pur_orders SET status='Partial' WHERE pur_order_id='".$order_id."'");
}
但是,plitems
表格中有2个订单项,第1行项目状态为Invoiced
,第2行项目状态为Partial
,但仍设置为pur_orders
状态为已开票而非Partial
答案 0 :(得分:1)
使用使用GROUP BY
的查询查找状态相同的订单。
UPDATE pur_orders AS p
CROSS JOIN (
SELECT MAX(invoice_status) AS invoice_status
FROM plitems
WHERE pur_order_id = $order_id
HAVING COUNT(DISTINCT invoice_status) = 1) AS t
SET p.status = t.invoice_status
WHERE p.pur_order_id = $order_id
如果只有一个状态,子查询将返回具有该状态的一行,然后将其分配给pur_orders.status
。如果有多个状态,子查询将不返回任何行,因此CROSS JOIN
将生成一个空的十字产品,并且它不会更新任何内容。