开发者

optimizing mysql if - else depend on my case

开发者 https://www.devze.com 2023-03-24 14:59 出处:网络
I\'m very newbie in mysql. pardon me if i just ask a newbie question here. in my case, I want to make branching in mysql like this :

I'm very newbie in mysql. pardon me if i just ask a newbie question here.

in my case, I want to make branching in mysql like this :

  1. if office_id is not null then is_invited = 1
  2. else if office_id = '0000' then is_invited = 1
  3. else is_invited = 0

after I saw @thomasrutter answer here " if else query in mysql "..

I made query like this on mysql :

select IF(y.office_id IS NOT NULL, 1, IF(y.office_id = '0000', 1, IF(y.office_id IS NULL, 0, 1))) as is_invited

my question is,, can I make the query to be more optimize (with respect 开发者_运维知识库to performance)

many thanks :)


Your logic doesn't make sense here.

Case 2 is always satisfied/never reached. If office_id is not null, then you already assign is_invited = 1. So office_id = '0000' will never be tested, since it's always not null. Really, your logic should simply be IF(y.office_id IS NOT NULL, 1, 0) as is_invited

0

精彩评论

暂无评论...
验证码 换一张
取 消