views:

81

answers:

2

I have this query:

(SELECT e.IdEvent,e.EventName,e.EventSubtitle,e.EventDescription,l.LocationName,e.EventVenue,EventStartDate,e.EventEndDate,e.EventHost,c.CategoryName,l.LocationCity,l.LocationState,e.isTBA,
(SELECT s.status FROM jos_rsevents_subscriptions s WHERE s.IdUser = 72 AND s.IdEvent = e.IdEvent LIMIT 1) as status 
FROM jos_rsevents_events e 
    LEFT JOIN jos_rsevents_locations l ON e.IdLocation=l.IdLocation 
    LEFT JOIN jos_rsevents_categories c ON e.IdCategory=c.IdCategory 
WHERE 1=1  AND status < 3 ) ORDER BY  EventStartDate

But I get the error. "unknown column 'status' in 'where clause'

How can I fix this?

+2  A: 

Try using HAVING instead, as that is applied after your subquery is run, e.g.:

(SELECT e.IdEvent,e.EventName,e.EventSubtitle,e.EventDescription,l.LocationName,e.EventVenue,EventStartDate,e.EventEndDate,e.EventHost,c.CategoryName,l.LocationCity,l.LocationState,e.isTBA,
(SELECT s.status FROM jos_rsevents_subscriptions s WHERE s.IdUser = 72 AND s.IdEvent = e.IdEvent LIMIT 1) as status 
FROM jos_rsevents_events e 
    LEFT JOIN jos_rsevents_locations l ON e.IdLocation=l.IdLocation 
    LEFT JOIN jos_rsevents_categories c ON e.IdCategory=c.IdCategory 
HAVING status < 3 ) ORDER BY  EventStartDate
Adam Bellaire
A: 

If table jos_rsevents_subscriptions has a primary key, you could also do this:

 SELECT e.IdEvent, e.EventName, e.EventSubtitle, e.EventDescription, 
     l.LocationName, e.EventVenue, EventStartDate, e.EventEndDate, 
     e.EventHost, c.CategoryName, l.LocationCity, l.LocationState, e.isTBA, 
     s.status     
 FROM jos_rsevents_events e     
    LEFT JOIN jos_rsevents_locations l 
       ON e.IdLocation=l.IdLocation     
    LEFT JOIN jos_rsevents_categories c 
       ON e.IdCategory=c.IdCategory 
    Left Join jos_rsevents_subscriptions s 
       On s.PK = (Select Max(PK) From jos_rsevents_subscriptions
                  Where IdUser = 72 
                     AND IdEvent = e.IdEvent)
 WHERE 1=1 AND status < 3  
 ORDER BY EventStartDate

P.S. Why the 1=1 ??

Charles Bretana
the previous programmer was lazy. There isn't always a WHERE clause so they added a 1=1 and all the possible WHERE clauses start with 'AND ...'
Malfist