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


190

因此,这就是我要在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
itemOrdered数据库具有一个唯一的ID itemsOrdered_ID,该ID称为且具有重复purchaseOrder_ID
John Ernest Guadalupe

1
purchaseorder另一方面,数据库具有唯一的IDpurchaseOrder_ID
John Ernest Guadalupe

Answers:


409

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

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
                     )

26
实际上,在MySQL中,假设在itemsOrdered.purchaseOrder_ID上有索引,则相关子查询应该是最有效的方法之一。
Gordon Linoff

8
@eggyal。。。我同意没有索引的相关版本的性能可能不如联接(取决于各种因素,例如行的乘法)。但是,对于索引,它应该比联接更好,因为它应该在第一次匹配时停止索引扫描。检查dev.mysql.com/doc/refman/5.5/en/…
Gordon Linoff 2012年

53

您可以使用多表UPDATE语法来实现ANTI-JOIN介于purchaseOrder和之间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

47

由于MySQL不支持if exists(*Your condition*) (*Write your query*),因此可以通过编写如下代码来实现“ if子句”:

(*Write your insert or update query*) where not exists (*Your condition*)

27

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

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

22
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)));

4
我正在努力查看这如何回答所有问题?
Mayer

1
@theMayer我也是,但这是一个很好的答案
Gabriel

2
恭喜,您的代码已被选为混淆代码。
Vishwanath Dalvi '18

1
我想这是我们可以使用的其他示例SQL
顶级大师

13
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

7
如果您也解释答案,那将很好,仅代码答案对将来的用户没有帮助
Kumar Saurabh 2015年

9

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

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/zh-cn/library/bb522522(v=sql.105).aspx上的正式文档


7

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

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

1

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

    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'        
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.