代码之家  ›  专栏  ›  技术社区  ›  Malfist

如何使用子查询中的变量?

  •  0
  • Malfist  · 技术社区  · 15 年前

    我有这个问题:

    (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
    

    但我得到了错误。”“Where子句”中的未知列“status”

    我怎么修这个?

    2 回复  |  直到 15 年前
        1
  •  2
  •   Adam Bellaire    15 年前

    试用使用 HAVING 相反,这是在运行子查询之后应用的,例如:

    (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
    
        2
  •  0
  •   Charles Bretana    15 年前

    中频表 jos_rsevents_subscriptions 有一个主键,您也可以这样做:

     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.为什么1=1??