Usage of MySQL's “IF EXISTS”

前端 未结 5 1980
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:39

    You cannot use IF control block OUTSIDE of functions. So that affects both of your queries.

    Turn the EXISTS clause into a subquery instead within an IF function

    SELECT IF( EXISTS(
                 SELECT *
                 FROM gdata_calendars
                 WHERE `group` =  ? AND id = ?), 1, 0)
    

    In fact, booleans are returned as 1 or 0

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

提交回复
热议问题