【问题标题】:mysql query filter out NULL or blank rowsmysql查询过滤掉NULL或空白行
【发布时间】:2020-04-23 13:40:35
【问题描述】:

我是编写 sql 的新手,所以提前感谢您提供的任何帮助。 我在 mysql 数据库中有一个表(table1),它的视图如下:

+-----+----------+------------+
| id  | key      | value      |
+-----+----------+------------+
|   1 | name     | Bob        |
|   1 | location | ABC        |
|   1 | date     | xxxx-xx-xx |
|   2 | name     | Jim        |
|   2 | location | MID        |
|   2 | date     |            |
|   3 | name     |            |
|   3 | location |            |
|   3 | date     |            |
|   4 | name     | Sue        |
|   4 | location | DFW        |
|   4 | date     | xxxx-xx-xx |
|   5 | name     | Sue        |
|   5 | location |            |
|   5 | date     | xxxx-xx-xx |
|   6 | name     | Bob        |
|   6 | location | GRE        |
|   6 | date     | xxxx-xx-xx |
|   7 | name     |            |
|   7 | location |            |
|   7 | date     |            |
+-----+----------+------------+

我创建了一个视图,我基本上将行反转(旋转)到如下所示的列:

+-----+-------+----------+------------+
| id  | name  | location | date       |
+-----+-------+----------+------------+
|   1 | Bob   | ABC      | xxxx-xx-xx |
|   2 | Jim   | MID      |            |
|   3 |       |          |            |
|   4 | Sue   | DFW      | xxxx-xx-xx |
|   5 | Sue   |          | xxxx-xx-xx |
|   6 | Bob   | GRE      | xxxx-xx-xx |
|   7 |       |          |            |
|   8 | Bob   | DFW      | xxxx-xx-xx |
|   9 |       |          |            |
|  10 | Joe   | DFW      | xxxx-xx-xx |
|  11 |       |          |            |
|  12 |       |          |            |
|  13 |       |          |            |
|  14 | Jim   | SUS      | xxxx-xx-xx |
+-----+-------+----------+------------+

这是我如何做到这一点的代码:

select c.`id`, max(ifnull(c.name,NULL)) as name, max(ifnull(c.location,NULL)) as location, max(ifnull(c.date,NULL)) as date from (
select `id`,
case when `key` = 'name' then value end as name,
case when `key` = 'location' then value end as location,
case when `key` = 'date' then value end as date
  from `table1`
  WHERE value != ''
) c group by `id`

我需要从视图中过滤掉名称、位置和日期为 NULL 或空白的行。通过添加WHERE value != '',我能够清除大约一半的行,但是如何过滤掉其余的行? WHERE value IS NOT NULL 似乎没有帮助。

【问题讨论】:

  • 请格式化问题以便我们阅读:)

标签: mysql null where-clause


【解决方案1】:

可能是您将空格作为值吗?请改用WHERE TRIM(value) != ''

试试这个? WHERE name != '' OR location != '' OR date != ''

【讨论】:

  • 这给了我与 WHERE value != ' ' 相同的结果
  • 我想知道是否因为表中附加到 id 的其他键具有值。如果我可以在 WHERE 中仅在名称、位置和日期中查找 value != ' ',那是否可以解决。不过我不知道怎么写。有什么建议吗?
【解决方案2】:

我不明白你为什么要这样使用IFNULL()
例如max(ifnull(c.name,NULL)) 等价于max(c.name)
但是您可以在 HAVING 子句中使用它来过滤掉 empty 行:

select c.`id`, 
  max(c.name) as name, 
  max(c.location) as location, 
  max(c.date) as date 
from (
select `id`,
  case when `key` = 'name' then value end as name,
  case when `key` = 'location' then value end as location,
  case when `key` = 'date' then value end as date
  from `table1`
  WHERE value != ''
) c group by `id`
having ifnull(name, '') <> '' or ifnull(location, '') <> '' or ifnull(date, '') <> ''

如果存在名称、位置或日期不只是空字符串而是包含空格的情况,那么也可以使用trim(),例如:

having ifnull(trim(name), '') <> '' or ifnull(trim(location), '') <> '' or ifnull(trim(date), '') <> ''

【讨论】:

    猜你喜欢
    • 2014-03-31
    • 2011-03-21
    • 1970-01-01
    • 2018-06-03
    • 2016-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多