2013-03-14 69 views

回答

1

如果你正在嘗試使用公用表表達式,然後loc_id將是可以像一個子查詢中使用的表,所以你可以這樣做:

with loc_id as (
    select l.location_id 
    from mopdb.lulocation l 
    where (l.location_short = 'FOO') 
) select 
    d.device 
from mopdb.ludevice d 
left join lulocation l on d.location_id = l.location_id 
where d.location_id = (select location_id from loc_id); 

然而,變量在SQL的每個方言中都會有不同的語法,因此它將在很大程度上取決於您使用的產品。

1

醜陋,但是:

SELECT d.device 
     FROM mopdb.ludevice d 
     LEFT JOIN lulocation l ON d.location_id = l.location_id 
     WHERE d.location_id = 
     (
     SELECT x.location_id 
     FROM mopdb.lulocation x 
     WHERE (x.location_short = 'FOO') 
    ) 
相關問題