简体   繁体   English

mysql 查询过滤掉 NULL 或空白行

[英]mysql query filter out NULL or blank rows

I am a novice at writing sql, so thank you in advance for any assistance you can provide.我是编写 sql 的新手,所以提前感谢您提供的任何帮助。 I have a table (table1) in a mysql database that views like this:我在 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     |            |
+-----+----------+------------+

I created a view where I basically invert (pivot) the rows to columns like this:我创建了一个视图,我基本上将行反转(旋转)到列,如下所示:

+-----+-------+----------+------------+
| 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 |
+-----+-------+----------+------------+

Here is the code for how I did this:这是我如何做到这一点的代码:

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`

I need to filter out the rows from the view where the name, location, and date are NULL or blank.我需要从视图中过滤掉名称、位置和日期为 NULL 或空白的行。 I was able to clear about half of the rows by adding the WHERE value != '' , but how do I filter out the rest?通过添加WHERE value != '' ,我能够清除大约一半的行,但是如何过滤掉 rest? WHERE value IS NOT NULL does not seem to help. WHERE value IS NOT NULL似乎没有帮助。

Could it be that you have spaces as value?难道你有空间作为价值? Try WHERE TRIM(value) != '' instead.尝试WHERE TRIM(value) != ''代替。

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

I don't understand why you use IFNULL() the way that you do.我不明白您为什么以这种方式使用IFNULL()
For example max(ifnull(c.name,NULL)) is equivalent to max(c.name) .例如max(ifnull(c.name,NULL))等价于max(c.name)
But you can use it in a HAVING clause to filter out the empty rows:但是您可以在HAVING子句中使用它来过滤掉行:

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, '') <> ''

If there is a case that name, location or date are not just empty strings but contain spaces then use also trim() like:如果在某些情况下,名称、位置或日期不仅是空字符串,而且包含空格,那么也可以使用trim() ,例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM