Comment utiliser une variable d'une sous-requête?
Question
J'ai cette requête:
(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
Mais je reçois l'erreur. "colonne" 'statut' inconnu dans la 'clause' où '
Comment puis-je résoudre ce problème?
La solution
Essayez d'utiliser HAVING
à la place, tel qu'il est appliqué après l'exécution de votre sous-requête, par exemple:
(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
Autres conseils
Si la table jos_rsevents_subscriptions
a une clé primaire, vous pouvez également le faire:
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. Pourquoi le 1 = 1 ??
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow