Question

I have a query like this -

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));

This one is giving me error 1054 unknown column e.id in order clause.

I was checking on google every where its naming mistake, but i am not getting how it is naming mistake please let me know if i am doing any thing wrong.

Was it helpful?

Solution

Try this by removing the e from ORDER BY clause since you are using the UNION statement:

ORDER BY id

instead of

ORDER BY e.id

OTHER TIPS

Your original question has been answered. e.id is no more available after UNION. Only id is.

However there is some things I would like to add:

  1. If I am not mistaken, the only difference between the first statement and the second is that in the first statement you require e.startdatetime to be less than NOW(). So the first statement retrieves a sub set of what the second statement retrieves. So you can remove the first statement completely including the union clause.
  2. You select only from table event. You order first by id. An id should be unique for a table. So the rest of the order by clause is just void; it won't change the order at all.
  3. In a comment to the first answer you got, you remark that you would like to order by a column, but you dont want to show it in your results. To achieve this do the following:

.

  select a, b, c
  from
  (
    select a, b, c, d 
    ... -- complete query here
  )
  order by d;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top