【问题标题】:error in case statement (ERROR 1241 (21000): Operand should contain 1 column(s))case 语句中的错误(错误 1241 (21000):操作数应包含 1 列)
【发布时间】:2018-02-08 05:54:13
【问题描述】:

我有一个表格,想要获取基于性别和分数的记录,比如分数超过 70 分时,男性数据将显示其他女性数据,我想使用案例陈述。 mysql查询是

select Parent_Id,Emp_Id,Emp_Gender,Emp_Score from EVENT_DETAIL where 
Emp_Score = (case when Emp_Score > '70' then (select * from EVENT_DETAIL 
where Emp_Gender = 'Male') else (select * from EVENT_DETAIL where 
Emp_Gender = 'Female') end); 

但在此操作数的错误应该包含 1 列

【问题讨论】:

  • CASE 表达式必须返回单个标量值。您正在返回整个 table (许多行,许多列)。您的查询毫无意义。
  • 您将 CASE 表达式与 CASE 语句混合在一起。

标签: mysql operands


【解决方案1】:

这是你想要的吗?

(select Parent_Id,Emp_Id,Emp_Gender,Emp_Score from EVENT_DETAIL where Emp_Score > 70 and Emp_Gender = 'Male')
union all
(select Parent_Id,Emp_Id,Emp_Gender,Emp_Score from EVENT_DETAIL where Emp_Score <= 70 and Emp_Gender = 'Female');

【讨论】:

  • 你说的查询我已经试过了,但我只想用case语句做
  • 新查询我已从 EVENT_DETAIL 中选择 * 其中 Emp_Score IN(当 Emp_Score >= '70' 时的情况下(选择 group_concat('''',Emp_Score,'''') as Emp_Score from EVENT_DETAIL其中 Emp_Gender = 'Male') else (select group_concat('''',Emp_Score,'''') as Emp_Score from EVENT_DETAIL where Emp_Gender = 'Female') end);现在越来越空了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-08
  • 1970-01-01
  • 2013-10-04
  • 2013-11-20
  • 2013-03-27
相关资源
最近更新 更多