Does MySQL Short Circuit the IF() function?

前端 未结 4 957
Happy的楠姐
Happy的楠姐 2021-01-18 15:05

I need to query data from a second table, but only if a rare set of conditions in the primary table is met:

SELECT ..., IF(a AND b AND c AND (SELECT 1 FROM t         


        
4条回答
  •  时光说笑
    2021-01-18 15:24

    It depends.

    IF doesn't short-circuit such that it can be used to avoid truncation warnings with GROUP_CONCAT, for example in:

    set @@group_concat_max_len = 5;
    
    select if(true or @var:=group_concat('warns if evaluated'), 'actual result', @var);
    

    the result will be 'actual result' but you'll get a warning:

    Warning (Code 1260): Row 1 was cut by GROUP_CONCAT()
    

    which is the same warning you get with less trivial GROUP_CONCAT expressions, such as distinct keys, and without the IF at all.

提交回复
热议问题