【问题标题】:Unknown Column Error Mysql未知列错误Mysql
【发布时间】:2017-01-19 10:29:04
【问题描述】:
SELECT u.uid,u.status,u.category,u.role, p.uname, p.photo, p.upos, p.city, p.state, p.country, p.services, p.slug, 
(select avg(rating) from rating where uid=u.uid) as rating 
FROM `hd-users` u 
JOIN `profile` p ON p.uid=u.uid 
WHERE u.status='1' AND u.role='C' AND rating >= 4

这是我的 SQL 查询,我正在加入三个表,在加入时我取第三个表的平均值。一切正常,但每当我尝试将平均评分值与数字进行比较时,我都会收到错误消息:Unknown column 'rating' in 'where clause'

【问题讨论】:

    标签: mysql


    【解决方案1】:

    错误是因为这行:

    AND rating >= 4
    

    这里,评分是由聚合函数生成的,您不能将 where 子句放在聚合列名称上。

    使用喜欢:

    having rating >= 4
    

    注意: WHERE 在聚合前过滤记录,HAVING 在聚合后起作用。

    【讨论】:

      【解决方案2】:

      它是p.ratingu.rating。您没有在此处指定您的评分:

      WHERE u.status='1' AND u.role='C' AND rating >= 4 // rating here has no alias
      

      【讨论】:

      • 检查这一行:(select avg(rating) from rating where uid=u.uid) as rating - 你不能在聚合列上设置条件
      猜你喜欢
      • 1970-01-01
      • 2016-04-01
      • 2012-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-01
      相关资源
      最近更新 更多