优化 mysql if - else 取决于我的情况

发布于 2024-11-27 15:02:58 字数 601 浏览 1 评论 0原文


我是 mysql 的新手。如果我只是在这里问一个新手问题,请原谅我。

就我而言,我想在mysql中进行这样的分支:

  1. 如果office_id不为空,则is_invited = 1,
  2. 否则如果office_id ='0000'然后 is_invited = 1
  3. 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 :

  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 :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黑寡妇 2024-12-04 15:02:58

你的逻辑在这里没有意义。

情况 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文