小编典典

如何在SQL字符串中放置“ if子句”?

sql

因此,这就是我要在 MySQL 数据库上执行的操作。

我想要做:

SELECT *
    FROM itemsOrdered
    WHERE purchaseOrder_ID = '@purchaseOrdered_ID'
        AND status = 'PENDING'

如果那不会返回任何行(可以通过)if(dr.HasRows == false),那么我现在将UPDATEpurchaseOrder数据库中创建一个:

UPDATE purchaseOrder
    SET purchaseOrder_status = 'COMPLETED'
    WHERE purchaseOrder_ID = '@purchaseOrder_ID'

我怎样才能使这个过程更短一些?


阅读 168

收藏
2021-04-15

共1个答案

小编典典

对于您的特定查询,您可以执行以下操作:

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
                     )
2021-04-15