215

所以这就是我想要在我的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'

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

4

9 回答 9

489

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

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
                     )
于 2012-12-21T14:23:30.693 回答
62

您可以使用多表UPDATE语法来实现ANTI-JOINbetween purchaseOrderand itemsOrdered

UPDATE purchaseOrder p LEFT JOIN itemsOrdered i
    ON p.purchaseOrder_ID = i.purchaseOrder_ID
   AND i.status = 'PENDING'
SET    p.purchaseOrder_status = 'COMPLETED'
WHERE  p.purchaseOrder_ID = '@purchaseOrder_ID'
   AND i.purchaseOrder_ID IS NULL
于 2012-12-21T14:25:08.523 回答
54

由于 MySQL 不支持if exists(*Your condition*) (*Write your query*),您可以通过这样编写来实现“if 子句”:

(*Write your insert or update query*) where not exists (*Your condition*)
于 2012-12-21T15:00:39.823 回答
30

您还可以使用以下查询来检查记录是否存在,然后对其进行更新:

if not exists(select top 1 fromFROM itemsOrdered
    WHERE purchaseOrder_ID = '@purchaseOrdered_ID'
        AND status = 'PENDING' )
Begin

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

End
于 2015-03-17T14:37:37.867 回答
26
Select FROM t1
    WHERE s11 > ANY
        (SELECT col1,col2 FROM t2
            WHERE NOT EXISTS
                (SELECT * FROM t3
                    WHERE ROW(5*t2.s1,77)=
                        (SELECT 50,11*s1 FROM t4 UNION SELECT 50,77 FROM
                            (SELECT * FROM t5) AS t5)));
于 2014-11-27T12:54:32.533 回答
16
if not exists(select top 1 fromFROM itemsOrdered
    WHERE purchaseOrder_ID = '@purchaseOrdered_ID'
        AND status = 'PENDING' )
Begin

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

End
于 2015-11-04T12:30:36.207 回答
11

sql server 2008 提供Merge基于单个匹配语句的插入、更新和删除操作后,也允许您加入。下面的示例示例可能会对您有所帮助。

MERGE Target AS T
USING Source AS S
ON (T.EmployeeID = S.EmployeeID) 
WHEN NOT MATCHED BY TARGET AND S.EmployeeName LIKE 'S%' 
    THEN INSERT(EmployeeID, EmployeeName) VALUES(S.EmployeeID, S.EmployeeName)
WHEN MATCHED 
    THEN UPDATE SET T.EmployeeName = S.EmployeeName
WHEN NOT MATCHED BY SOURCE AND T.EmployeeName LIKE 'S%'
    THEN DELETE 
OUTPUT $action, inserted.*, deleted.*;

像这样,您可以在一个语句中插入、更新和删除。

有关更多信息,您可以参考 https://technet.microsoft.com/en-us/library/bb522522(v=sql.105).aspx上的官方文档

于 2016-12-08T06:42:33.487 回答
10

如果表包含数百万条记录,则以下查询将快速运行。

UPDATE PO
SET PO.purchaseOrder_status = 'COMPLETED'
FROM purchaseOrder PO
LEFT OUTER JOIN itemsOrdered IOD ON IOD.purchaseOrder_ID = PO.purchaseOrdered_ID and IOD.status = 'PENDING'
WHERE IOD.purchaseOrder_ID IS NULL
于 2018-05-10T09:55:48.380 回答
3

您可以声明一个变量来保存选择查询的返回结果数。如果此变量大于 0,则可以运行更新语句

    Declare @ResultCount int
    SELECT @ResultCount = count(*) FROM itemsOrdered WHERE purchaseOrder_ID = '@purchaseOrdered_ID' AND status = 'PENDING'        
    If @ResultCount > 0
UPDATE purchaseOrder SET purchaseOrder_status = 'COMPLETED' WHERE purchaseOrder_ID = '@purchaseOrder_ID'        
于 2019-11-26T12:41:39.290 回答