错误-1054未知列e.id in order子句

时间:2014-03-26 08:11:28

标签: mysql sql union

我有这样的查询 -

SELECT e.id FROM event e WHERE e.startdatetime<NOW() AND e.isEventDeleted=FALSE AND e.isNeighborlyInvited=TRUE AND e.organized_by!=49 AND  e.event_address IN (SELECT id FROM address a WHERE latitude!='' AND longitude!='' AND IFNULL(( 3959 * ACOS( COS( RADIANS(22.6979425) ) * COS( RADIANS( latitude ) ) * COS( RADIANS( longitude ) - RADIANS(75.8597305) ) + SIN( RADIANS(22.6979425) ) * SIN( RADIANS( latitude ) ))),0)<100) AND e.id NOT IN (SELECT eventid FROM event_interest WHERE approvalStatus!='InterestExpressed' AND interested_user=49) AND e.id NOT IN (SELECT eventid FROM event_invite WHERE invited_user=49 )      
UNION
SELECT e.id FROM event e WHERE  e.isEventDeleted=FALSE AND e.isNeighborlyInvited=TRUE AND e.organized_by!=49 AND  EXISTS (SELECT id FROM address a WHERE latitude!='' AND longitude!='' AND e.event_address=id AND IFNULL(( 3959 * ACOS( COS( RADIANS(22.6979425) ) * COS( RADIANS( latitude ) ) * COS( RADIANS( longitude ) - RADIANS(75.8597305) ) + SIN( RADIANS(22.6979425) ) * SIN( RADIANS( latitude ) ))),0)<100) AND NOT EXISTS (SELECT eventid FROM event_interest WHERE e.id =eventid AND approvalStatus!='InterestExpressed' AND interested_user=49) AND NOT EXISTS (SELECT eventid FROM event_invite WHERE eventid=e.id AND invited_user=49 )  
ORDER BY e.id,((SELECT AVG(avgr.abc) AS VALUE FROM ( SELECT YEAR(NOW())-YEAR(dob) AS abc FROM user_detail WHERE userid=(SELECT interested_user FROM event_interest  WHERE eventid=477 AND approvalStatus='Approve') UNION SELECT YEAR(NOW())-YEAR(dob) AS abc FROM user_detail WHERE userid=(SELECT invited_user FROM event_invite WHERE eventid=477  AND acceptance='Accept')) AS avgr)+(SELECT IFNULL(( 3959 * ACOS( COS( RADIANS(22.6979425) ) * COS( RADIANS( latitude ) ) * COS( RADIANS( longitude ) - RADIANS(75.8597305) ) + SIN( RADIANS(22.6979425) ) * SIN( RADIANS( latitude ) ))),0) FROM address a WHERE latitude!='' AND longitude!='' AND e.event_address=id));

这个在订单条款中给了我错误1054未知列e.id.

我正在检查谷歌的每一个命名错误,但我不知道如何命名错误请告诉我,如果我做错了什么。

2 个答案:

答案 0 :(得分:6)

通过从ORDER BY子句中删除e来尝试此操作,因为您使用的是 UNION 语句:

ORDER BY id

而不是

ORDER BY e.id

答案 1 :(得分:0)

您的原始问题已得到解答。在UNION之后,e.id不再可用。只有id是。

但是我想补充一些内容:

  1. 如果我没有弄错,第一个语句和第二个语句之间的唯一区别是,在第一个语句中,您需要e.startdatetime小于NOW()。因此,第一个语句检索第二个语句检索的子集。因此,您可以完全删除第一个语句,包括union子句。
  2. 您只能从表格事件中选择。您首先按ID订购。对于表,id应该是唯一的。因此,order by子句的其余部分只是无效;它根本不会改变订单。
  3. 在您收到的第一个答案的评论中,您注意到您想按列排序,但您不想在结果中显示它。要实现此目的,请执行以下操作:
  4.   select a, b, c
      from
      (
        select a, b, c, d 
        ... -- complete query here
      )
      order by d;