如何从sql中删除空值

时间:2016-10-07 06:25:10

标签: sql

我试图执行此代码来过滤数据

select 
   max(case when meta_key = 'miglad_firstname' then meta_value end) as f_name,
   max(case when meta_key = 'miglad_lastname' then meta_value end) as l_name,
   max(case when meta_key = 'miglad_country' then meta_value end) as country,
   post_id

from sw_postmeta where group by post_id  order by f_name DESC;

post_Id用于对表的数据进行分组但问题在于它向我显示了如此多的Null字段 我的专栏看起来像这样

post_Id  |  meta_key  | meta_value 
    3      |  miglad_firstname     | John            
    3        |  miglad_lastname       | Arya
    3        | miglad_country      | India
   4         | miglad_firstname       | Ram
   4         |   miglad_lastname      | Singh
  4          | miglad_country       | Sri Lanka

但结果显示如下

 F_name  |   l_name    |   country
  John       | Arya          |  India
   Ram       |   Singh       |  Shri Lanka 
   Null        |  Null            |  Null
  Null       |  Null             | Null
  Null       |  Null             | Null
  Null       |  Null             | Null.....

所以我想从sql中删除这些null结果。那么我该如何做到这一点请帮助

1 个答案:

答案 0 :(得分:0)

从您的选择查询中移除post_id,您应该得到所需的结果。

select 
   max(case when meta_key = 'miglad_firstname' then meta_value end) as f_name,
   max(case when meta_key = 'miglad_lastname' then meta_value end) as l_name,
   max(case when meta_key = 'miglad_country' then meta_value end) as country,
from sw_postmeta where group by post_id  order by f_name DESC;