【问题标题】:SQL Select one statement expect values start withSQL 选择一条语句期望值以
【发布时间】:2019-03-04 11:13:45
【问题描述】:

我这里有以下代码:

SELECT productcode, Brand, product, size
from outerbarcodes i
where productcode = '' or productcode is null or
     brand = '' or brand is null or
     product = '' or product is null or
     size = '' or size is null;

我想添加一个条件,不包括以'PK' 开头的值在列产品代码中。如何修改代码?

提前致谢

【问题讨论】:

  • @vc74 MySQL 不能那样工作。
  • 我将其引用到 MySQL Workbench

标签: mysql sql


【解决方案1】:

您可以在当前的 where 子句中添加 LIKE 条件:

SELECT productcode, Brand, product, size
FROM outerbarcodes i
WHERE
    (productcode > '' AND productcode NOT LIKE 'PK%') OR
    brand > '' OR
    product > '' OR
    size > '';

【讨论】:

  • 我刚刚对其进行了测试,使用不像 'pk%' 这样的运算符确实有意义。它仍然返回以“PK”开头的值。我要读一下这个stackoverflow.com/questions/40376260/… 与空值有关的东西......
  • @Eduards 您应该将示例数据添加到您的问题中。我的逻辑在我看来是正确的,至少基于我对您问题的解释。
  • 我完全同意蒂姆的观点,我相信它与导致此问题的空值有关。我已经用我刚刚使用的代码发布了答案。它工作得很好。我只需要修改它以不显示空值
【解决方案2】:

使用not like 运算符

`SELECT productcode, Brand, product, size
 from outerbarcodes i
 where ((productcode = '' or productcode is null) and productcode not like 'PK%') or
      (brand = '' or brand is null) or
      (product = '' or product is null) or
      (size = '' or size is null);`

【讨论】:

  • 你不需要所有这些括号 :-)
【解决方案3】:

我想你想要:

select productcode, Brand, product, size
from outerbarcodes i
where (productcode = '' or productcode is null or
       brand = '' or brand is null or
       product = '' or product is null or
       size = '' or size is null
      ) and
      productcode not like 'PK%';

我对其他答案有点困惑,因为他们正在更改其他列的比较 - 他们不是检查空列,而是检查具有值的列。

【讨论】:

  • 抱歉,您刚才给出的代码是正确的。抱歉没有意识到发生了什么。很好地发现了其他答案
  • from outerbarcodes i之后的'i'代表什么
  • @Eduards 。 . .它是一个表别名。您可以使用它来限定列名,例如i.productcode。因为查询只有一个表引用,所以限定列名是可选的。
  • 知道这对我很有用,谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-13
  • 2011-06-09
  • 1970-01-01
相关资源
最近更新 更多