【问题标题】:How to fix 'Selecting rows without null or blank value in each column'?如何修复“选择每列中没有空值或空白值的行”?
【发布时间】:2019-08-29 09:09:15
【问题描述】:

我在这里使用此代码时遇到问题:

select *
from toadd
where pcode = '' or pcode is null or
      brand = '' or brand is null or
      description = '' or description is null;

我正在尝试返回在上面的列中没有空值或空白值的值。

我的问题是:我已将查询编码为仅返回只有空值或空白值的值,看看它是否真的有效,然后我将使用基于该查询的删除语句。

但它仍然返回带有填充数据的列,即使我说过不显示查询中填充的任何值。

不知道为什么要这样做?

我所有的列类型都设置为VARCHAR 我将数据与文本和数字混合在一起。

这是我的表格布局:

Table: outerb
Columns:
id int(11) AI PK 
pcode varchar(255) 
brand varchar(255) 
description varchar(255) 
size varchar(255) 
barcode varchar(255)

这就是现在的样子

enter image description here

这是我的预期结果:

enter image description here

【问题讨论】:

  • 添加一个拒绝零长度字符串的约束。
  • 根据您最近添加的预期输出照片,您实际上不希望任何结果会返回其中三个('' 或 NULL),对吗?
  • 是的,那是正确的,让我稍微重写一下我的问题。谢谢你

标签: mysql sql mysql-workbench


【解决方案1】:

将外部or 条件更改为and

     select * from toadd where (pcode = '' or pcode is null) and
     ( brand = '' or brand is null) and
      (description = '' or description is null)

【讨论】:

  • 这不会返回 0 行。我已经更新了我的问题并添加到我的表格结构中
【解决方案2】:

如果您想要所有测试列不为空或不为空的行

select * 
from toadd 
where !(pcode = '' or pcode is null) AND
      !(brand = '' or brand is null) AND
      !(description = '' or description is null);

如果要排除其中一列或多列为空或为空的行

select * 
from toadd 
where !(pcode = '' or pcode is null) OR
      !(brand = '' or brand is null) OR
      !(description = '' or description is null);

【讨论】:

  • 嗨,这个查询返回 0 个值。我附上了一些有问题的图片并稍微更新了问题以便更好地理解
  • 快速提问! 是做什么的?
  • 它是一个否定,与NOT相同。
【解决方案3】:

您可以使用以下 SQL 查询获得所需的结果

SELECT * FROM `outerb ` 
WHERE Length(`pcode `) > 0 
AND Length(`brand `) >0 
AND Length(`description `) > 0

【讨论】:

    猜你喜欢
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-19
    • 2022-01-23
    • 2013-06-13
    • 2021-11-15
    • 2016-06-13
    • 1970-01-01
    相关资源
    最近更新 更多