Usage of MySQL's “IF EXISTS”

前端 未结 5 2000
Happy的楠姐
Happy的楠姐 2020-11-22 13:25

Here are two statements that I\'d like to work, but which return error messages:

IF EXISTS (SELECT * FROM gdata_calendars WHERE `group` =  ? AND id = ?) SELE         


        
5条回答
  •  北恋
    北恋 (楼主)
    2020-11-22 13:54

    The accepted answer works well and one can also just use the

    If Exists (...) Then ... End If; 
    

    syntax in Mysql procedures (if acceptable for circumstance) and it will behave as desired/expected. Here's a link to a more thorough source/description: https://dba.stackexchange.com/questions/99120/if-exists-then-update-else-insert

    One problem with the solution by @SnowyR is that it does not really behave like "If Exists" in that the (Select 1 = 1 ...) subquery could return more than one row in some circumstances and so it gives an error. I don't have permissions to respond to that answer directly so I thought I'd mention it here in case it saves someone else the trouble I experienced and so others might know that it is not an equivalent solution to MSSQLServer "if exists"!

提交回复
热议问题