Mysql where 子句 union all 中的未知列

Mysql Unknown column in where clause union all

仍然与我的 有关,有一个像这样的 table(tb_data)

+---------+---------------------+---------------------+---------------------+---------------------+-------+ 
| Disease | Additional_Disease1 | Additional_Disease2 | Additional_Disease3 | Additional_Disease4 | Room  |
+---------+---------------------+---------------------+---------------------+---------------------+-------+
| A01     | A03                 | A02                 |                     |                     | Man   |
| A03     | A02                 |                     |                     |                     | Woman |
| A03     | A05                 |                     |                     |                     | Child |
| A03     | A05                 |                     |                     |                     | Man   |
| A02     | A05                 | A01                 | A03                 |                     | UGD   |
+---------+---------------------+---------------------+---------------------+---------------------+-------+ 

我的问题是如何做到这一点

+---------+-------+ 
| Disease | Total |
+---------+-------+
| A03     | 2     |
| A02     | 1     |
| A01     | 1     |
| A05     | 1     |
+---------+-------+

这是我的代码尝试

    select Disease, count(*) total
    from (
    select Disease from tb_data
    union all select Additional_Disease1 from tb_data
    union all select Additional_Disease2 from tb_data
    union all select Additional_Disease3 from tb_data
    union all select Additional_Disease4 from tb_data
) t
where Disease is not Null
and Room = 'Man'
group by Disease
order by total desc, Disease

导致错误的原因

Error Code: 1054. Unknown column 'Room' in 'where clause'

您的问题是派生的 table 不包含 Room 列。您可以在派生的 table:

中按 Room 过滤
select Disease, count(*) total
from (
    select Disease from tb_data where Room = 'Man'
    union all select Additional_Disease1 from tb_data where Room = 'Man'
    union all select Additional_Disease2 from tb_data where Room = 'Man'
    union all select Additional_Disease3 from tb_data where Room = 'Man'
    union all select Additional_Disease4 from tb_data where Room = 'Man'
) t
where Disease is not Null
group by Disease
order by total desc, Disease

或在派生的 table:

中包含 Room
select Disease, count(*) total
from (
    select Room, Disease from tb_data
    union all select Room, Additional_Disease1 from tb_data
    union all select Room, Additional_Disease2 from tb_data
    union all select Room, Additional_Disease3 from tb_data
    union all select Room, Additional_Disease4 from tb_data
) t
where Disease is not Null
and Room = 'Man'
group by Disease
order by total desc, Disease

如果 tb_data 很大或由子查询生成,那么我不推荐使用 union all 方法来反透视它。相反:

select (case n.n
           when 1 then Disease
           when 2 then Additional_Disease1
           when 3 then Additional_Disease2
           when 4 then Additional_Disease3
           when 5 then Additional_Disease4
        end) as the_disease, count(*)
from tb_data d cross join
     (select 1 as n union all
      select 2 as n union all
      select 3 as n union all
      select 4 as n union all
      select 5 as n
     ) n
where Room = 'Man'
group by the_disease
having the_disease is not null