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

时间:2012-12-21 14:21:21

标签: mysql sql

所以这就是我想在 MySQL 数据库上做的事情。

我想这样做:

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

如果这不会返回任何行,这可以通过if(dr.HasRows == false),我现在可以在UPDATE数据库中创建purchaseOrder

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

我怎样才能让这个过程变得更短?

9 个答案:

答案 0 :(得分:340)

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

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
                     )

答案 1 :(得分:47)

您可以使用多表UPDATE语法在ANTI-JOINpurchaseOrder之间生成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

答案 2 :(得分:42)

由于MySQL不支持if exists(*Your condition*) (*Write your query*),你可以这样编写'if子句':

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

答案 3 :(得分:21)

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

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

答案 4 :(得分:17)

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

答案 5 :(得分:9)

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

答案 6 :(得分:6)

在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

答案 7 :(得分:2)

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

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

答案 8 :(得分:0)

您可以声明一个变量,该变量保存选择查询中返回的结果数。 如果此变量大于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'