Mysql Unknown column in where 子句 union all
Posted
技术标签:
【中文标题】Mysql Unknown column in where 子句 union all【英文标题】:Mysql Unknown column in where clause union all 【发布时间】:2020-03-07 08:07:02 【问题描述】:仍然与我的Previous Question 相关,有一个像这样的表(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'
【问题讨论】:
任何时候你发现自己有枚举列名(上面说'2'),你应该考虑你的架构设计是否真的是最优的 最佳?你能详细说明/举个例子吗?我对编程有点陌生,所以我仍然不太了解...... 您的设计应该类似于 Nick 的子查询的输出 【参考方案1】:您的问题是您的派生表不包含 Room
列。您可以在派生表中按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
或者在派生表中包含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
【讨论】:
@KanienNotiros 不用担心 - 我很高兴能提供帮助。【参考方案2】:如果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
【讨论】:
以上是关于Mysql Unknown column in where 子句 union all的主要内容,如果未能解决你的问题,请参考以下文章
Mysql Unknown column in where 子句 union all
mysql中:Unknown column 'tt' in 'field list'
mysql 的 Unknown column '6' in 'order clause' 这是啥错误啊!! 高手们帮个忙啊!!!
Python Flask 向MySQL表里插入一条记录,提示Unknown column 'XXX' in 'field list
MySQL 报错ERROR 1054 (42S22): Unknown column 'plugin' in 'mysql.user'
Mysql Error Code: 1054. Unknown column 'typeName' in 'field list'