where子句中的未知列,带有case + union + like

时间:2017-08-07 15:17:15

标签: mysql sql hibernate jdbctemplate

我正在使用mysql中的sql查询。我的查询是这样的,

select reqId,sDate,stitle,sType,status,reqUser,assignedTo from 
(select request_id as reqId,date as sDate,title as sTitle,type as sType,status as status,request_user as reqUser ,
case when status='posted' then fbuser.name
     when status='awaiting approval' then fbmanager.name
     when status='approved' then fbAdmin.name
     when status='rejected' then fbAdmin.name
else null
end as assignedTo
from fb_requests fr
left join users fbuser on (fr.reqUser = fbuser.id)
left join users fbmanager on (fr.managerid= fbmanager.id)
left join users fbAdmin on (fr.adminin = fbAdmin.id)
where 1=1
and ( upper(assignedTo) like upper('abc') )
UNION
select request_id as reqId,date as sDate,title as sTitle,type as sType,status as status,request_user as reqUser ,
case when status='posted' then fbuser.name
     when status='awaiting approval' then fbmanager.name
else null
end as assignedTo
from fb_wall_posts fw
left join users fbuser on (fw.request_by_id = fbuser.id)
left join users fbmanager on (fw.manager_id= fbmanager.id)
 where 1=1
and ( upper(assignedTo) like upper('abc') ))

这里我得到'未知列分配给'。任何人都可以帮我解决这个问题。提前感谢您的回复。

3 个答案:

答案 0 :(得分:1)

您不能在where条件中使用别名(您可以在需要时按顺序使用别名和在某些mysql版本中使用别名)必须重复代码

  select reqId,sDate,stitle,sType,status,reqUser, assignedTo from 
  (select request_id as reqId,date as sDate,title as sTitle,type as sType,status as status,request_user as reqUser ,
  case when status='posted' then fbuser.name
       when status='awaiting approval' then fbmanager.name
       when status='approved' then fbAdmin.name
       when status='rejected' then fbAdmin.name
  else null
  end as assignedTo
  from fb_requests fr
  left join users fbuser on (fr.reqUser = fbuser.id)
  left join users fbmanager on (fr.managerid= fbmanager.id)
  left join users fbAdmin on (fr.adminin = fbAdmin.id)
  where 1=1
  and ( upper(
        case when status='posted' then fbuser.name
           when status='awaiting approval' then fbmanager.name
           when status='approved' then fbAdmin.name
           when status='rejected' then fbAdmin.name
      else null
      end
  ) like upper('abc') )
  UNION
  select request_id as reqId,date as sDate,title as sTitle,type as sType,status as status,request_user as reqUser ,
  case when status='posted' then fbuser.name
       when status='awaiting approval' then fbmanager.name
  else null
  end as assignedTo
  from fb_wall_posts fw
  left join users fbuser on (fw.request_by_id = fbuser.id)
  left join users fbmanager on (fw.manager_id= fbmanager.id)
   where 1=1
  and ( upper(
    case when status='posted' then fbuser.name
       when status='awaiting approval' then fbmanager.name
      else null
    end
    ) like upper('abc') ))

答案 1 :(得分:1)

当where子句执行时,AssignedTo尚未被定义,因此你必须使用完整的case语句,因为别名尚未创建。

select reqId,sDate,stitle,sType,status,reqUser,assignedTo from 
(select request_id as reqId,date as sDate,title as sTitle,type as sType,status as status,request_user as reqUser ,
case when status='posted' then fbuser.name
     when status='awaiting approval' then fbmanager.name
     when status='approved' then fbAdmin.name
     when status='rejected' then fbAdmin.name
else null
end as assignedTo
from fb_requests fr
left join users fbuser on (fr.reqUser = fbuser.id)
left join users fbmanager on (fr.managerid= fbmanager.id)
left join users fbAdmin on (fr.adminin = fbAdmin.id)
where 1=1
and ( upper(case when status='posted' then fbuser.name
     when status='awaiting approval' then fbmanager.name
     when status='approved' then fbAdmin.name
   ) like upper('abc') )
UNION
select request_id as reqId,date as sDate,title as sTitle,type as sType,status as status,request_user as reqUser ,
case when status='posted' then fbuser.name
     when status='awaiting approval' then fbmanager.name
else null
end as assignedTo
from fb_wall_posts fw
left join users fbuser on (fw.request_by_id = fbuser.id)
left join users fbmanager on (fw.manager_id= fbmanager.id)
 where 1=1
and ( upper(case when status='posted' then fbuser.name
     when status='awaiting approval' then fbmanager.name
     when status='approved' then fbAdmin.name
   ) like upper('abc') ))

答案 2 :(得分:0)

看起来您在UNION关键字之前缺少右括号。