where子句中的未知列

时间:2014-03-26 11:24:05

标签: mysql sql union

我有这样的查询 -

SELECT e.id, 
       ( (SELECT ABS((SELECT YEAR(NOW()) - YEAR(ud.dob) 
                      FROM   user_detail ud 
                      WHERE  ud.userid = 49) - AVG(avgr.abc 
                                                   + (SELECT YEAR(NOW()) - YEAR(ud.dob) 
                                                      FROM   user_detail ud 
                                                      WHERE  ud.userid = 49))) AS VALUE 
          FROM   (SELECT YEAR(NOW()) - YEAR(ud.dob) AS abc 
                  FROM   user_detail ud 
                  WHERE  ud.userid = (SELECT ei.interested_user 
                                      FROM   event_interest ei 
                                      WHERE  ei.eventid = e.id 
                                             AND ei.approvalstatus = 'Approve') 
                  UNION 
                  SELECT YEAR(NOW()) - YEAR(ud.dob) AS abc 
                  FROM   user_detail ud 
                  WHERE  ud.userid = (SELECT ei.invited_user 
                                      FROM   event_invite ei 
                                      WHERE  ei.eventid = e.id 
                                             AND ei.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  a.latitude != '' 
                   AND a.longitude != '' 
                   AND e.event_address = a.id) ) AS dest 
FROM   event e 
WHERE  ( ( e.maximumattendeesallow > ( (SELECT COUNT(*) AS cnt 
                                        FROM   event_interest 
                                        WHERE  eventid = e.id 
                                               AND approvalstatus = 'Approve') 
                                       + (SELECT COUNT(*) AS cnt 
                                          FROM   event_invite 
                                          WHERE  eventid = e.id 
                                                 AND acceptance = 'Accept') ) ) 
          OR ( e.maximumattendeesallow = 0 ) ) 
       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); 

此查询给我错误 -

Error Code: 1054
Unknown column 'e.id' in 'where clause'

为什么它不允许子查询在order by子句中使用e.id.

2 个答案:

答案 0 :(得分:0)

在查询中,您使用了 E.ID 。将其更改为“ e.id ”并执行。这次你不会得到那个错误。

答案 1 :(得分:0)

在第四个选择子句中,即在AND NOT EXISTS之后(选择..,。在你提到的where子句为 WHERE e.id = eventid ,而不是这个,写为 WHERE eventid = e.id 。这将解决您的错误。