Update and left outer join statements

前端 未结 5 935
被撕碎了的回忆
被撕碎了的回忆 2021-02-06 23:45

I have two tabels with a relation and I want to update a field in table A. Is it possible to combine update and join in the same query? I googled it but didnt find any working s

5条回答
  •  情歌与酒
    2021-02-07 00:00

    The Left join in this query is pointless:

    UPDATE md SET md.status = '3' 
    FROM pd_mounting_details AS md 
    LEFT OUTER JOIN pd_order_ecolid AS oe ON md.order_data = oe.id
    

    It would update all rows of pd_mounting_details, whether or not a matching row exists in pd_order_ecolid. If you wanted to only update matching rows, it should be an inner join.

    If you want to apply some condition based on the join occurring or not, you need to add a WHERE clause and/or a CASE expression in your SET clause.

提交回复
热议问题