优化 mysql if - else 取决于我的情况
我是 mysql 的新手。如果我只是在这里问一个新手问题,请原谅我。
就我而言,我想在mysql中进行这样的分支:
- 如果office_id不为空,则is_invited = 1,
- 否则如果office_id ='0000'然后 is_invited = 1
- else is_invited = 0
在我看到@thomasrutter在这里回答“ mysql 中的 if else 查询 "..
我在 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
我的问题是,我可以使查询更加优化(就性能而言),
非常感谢:)
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 :
- if office_id is not null then is_invited = 1
- else if office_id = '0000' then is_invited = 1
- 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 :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的逻辑在这里没有意义。
情况 2 总是满足/从未达到。如果office_id不为空,那么您已经指定了is_invited = 1。因此office_id = '0000'将永远不会被测试,因为它始终不为空。实际上,您的逻辑应该只是
IF(y.office_id IS NOT NULL, 1, 0) as is_invited
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