【问题标题】:MySQL Opposite of ConditionMySQL 条件相反
【发布时间】:2016-10-03 12:41:51
【问题描述】:

我知道在其他语言中,您可以使用某些东西(通常是 !)来表示以下内容的反义词。有没有办法在 MySQL 中做这样的事情?

例如,我有这个查询来选择不为空或空白的所有内容:

select * from `invoices` where `Notes`>'';

但是有没有办法做相反的事情?因为目前我只知道可以重写这样的查询

select * from `invoices` where ifnull(`Notes`,'')='';

但这消除了在Notes 列上或以这种方式建立索引的机会

select * from `invoices` where (`Notes` is null or `Notes`=''); 

但是那个比类似的要长得多

select * from `invoices` where !`Notes`>'';

这将是理想的。

【问题讨论】:

  • 为什么不将笔记设为“不可为空”,默认值为“”?
  • where not notes >'';虽然我不确定这是否会使用索引。

标签: mysql sql select logical-operators


【解决方案1】:

SQL 有一个not 运算符:

SELECT * FROM `invoices` WHERE NOT (`Notes`>'');
-- Here -----------------------^

【讨论】:

  • 嗯...我似乎遗漏了一些东西。当我这样做时,我没有得到任何结果,这不是我对 null 字段的想法吗?
  • 此解决方案似乎与select * from `invoices` where `Notes`<=''; 的作用相同,但不适用于我的null
  • 那么是否有一个实际的解决方案可以使用NULL 值,或者没有?
猜你喜欢
  • 2016-09-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-17
  • 2016-06-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多