0

这就是我想要做的

WITH 
loc_id AS ( // take the result of the following select statement
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 = loc_id) //use it here 
4

2 回答 2

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 方言中都有不同的语法,因此它在很大程度上取决于您使用的产品。

于 2013-03-14T22:23:48.987 回答
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')
      )
于 2013-03-14T22:27:16.120 回答