因此,这就是我要在 MySQL 数据库上执行的操作。
我想要做:
SELECT * FROM itemsOrdered WHERE purchaseOrder_ID = '@purchaseOrdered_ID' AND status = 'PENDING'
如果那不会返回任何行(可以通过)if(dr.HasRows == false),那么我现在将UPDATE在purchaseOrder数据库中创建一个:
if(dr.HasRows == false)
UPDATE
purchaseOrder
UPDATE purchaseOrder SET purchaseOrder_status = 'COMPLETED' WHERE purchaseOrder_ID = '@purchaseOrder_ID'
我怎样才能使这个过程更短一些?
对于您的特定查询,您可以执行以下操作:
UPDATE purchaseOrder SET purchaseOrder_status = 'COMPLETED' WHERE purchaseOrder_ID = '@purchaseOrder_ID' and not exists (SELECT * FROM itemsOrdered WHERE purchaseOrder_ID = '@purchaseOrdered_ID' AND status = 'PENDING' )
但是,我可能会猜测您在更高级别进行循环。要设置所有此类值,请尝试以下操作:
UPDATE purchaseOrder SET purchaseOrder_status = 'COMPLETED' WHERE not exists (SELECT 1 FROM itemsOrdered WHERE itemsOrdered.purchaseOrder_ID = purchaseOrder.purchaseOrdered_ID AND status = 'PENDING' limit 1 )