mysql5.7|sql语句求解

问题遇到的现象和发生背景

查询出表中这些不为空的,我用is not null 判断,
select后面不知道该写啥了

问题相关代码,请勿粘贴截图
SELECT
    
FROM
    report_zdlz_data 
WHERE
    years = '2022'
    and htz_count_last  is not null
    and    zdlz_count1  is not null
    and    zdlz_count2 is not null
    and    zdlz_count3 is not null
    and    zdlz_count4 is not null
    and    zdlz_count5 is not null
    and    zdlz_count6 is not null
    and    zdlz_count7 is not null
    and    zdlz_count8 is not null
    and    zdlz_count9 is not null
   and    zdlz_count10 is not null
运行结果及报错内容

img

我想要达到的结果

只想查出这些不为空的列,

img

SELECT
*
FROM
report_zdlz_data
WHERE
years = '2022'
and htz_count_last is not null
and zdlz_count1 is not null
and zdlz_count2 is not null
and zdlz_count3 is not null
and zdlz_count4 is not null

这样试一下啊, 因为 zdlz_count5 以后的列都是null数据,加上 is not null条件后,肯定查不到来啊



```sql
SELECT
    *
FROM
    report_zdlz_data 
WHERE
    years = '2022'
    and    zdlz_count5 is  null
    and    zdlz_count6 is  null
    and    zdlz_count7 is  null
    and    zdlz_count8 is  null
    and    zdlz_count9 is  null
   and    zdlz_count10 is  null

```

看你是查这些列是全不为空,还是 某个字段有值就输出, 某个字段有值就输出把and 改成 or 就行了

把关键字and换成or and是必须都成立

你这么写查出来肯定为空,因为你这些字段都是并列的,只要有一列全是null,你查出来的结果都是null;
不能再where后面用and连接,再select 后吗写判断,用 case when

不可能用一条sql达到你想要的效果。 至少需要通过sql查出哪一列所有值为null 然后重新再写一条sql 在select后面人为的去掉那个字段