dongpin4611 2017-05-04 12:34
浏览 34
已采纳

MySQL不存在未知列错误

for a couple of hours I have been working on this problem.

I have a table in MySQL called person_state

----------
person_state
----------------------------
id | person_id | state | date
----------------------------

I need to get active persons last activation dates, The state of every person is has been changed many times(States are : active, passive, waiting, blocked). If a person is activated then deactivated, my query should not get it.

My query is

select id as activation_id, person_id as active_person_id
  from person_state
  where state = 'active' 
  and
  not exists(
         select * from person_state 
         where person_id = active_person_id
         and
         id > activation_id
    ) 

I am getting error Unknown column 'active_person_id' in 'where clause' .

Thanks for your your time

  • 写回答

3条回答 默认 最新

  • douren6874 2017-05-04 12:36
    关注

    WHERE gets executed before SELECT and hence, you can't use column aliases inside it, try with column name instead, e.g.:

    select id as activation_id, person_id as active_person_id
      from person_state ps
      where state = 'active' 
      and
      not exists(
             select id from person_state 
             where person_id = ps.person_id
             and
             id > ps.activation_id
        )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?