sql 来自哪里的子查询
sql subquery from where in
我正在按照这些思路写一些东西:
select fielda,fieldb,
(select sum(field1)-sum(field2) as fieldc
from tableb
where fieldid = list_of_ids[i])
from tablea
where fieldid in (list_of_ids);
在子查询中,我想使用外部 where 子句中的值。因此,如果 list_of_ids 是 123,456,789,子查询中的 fieldid 将是 123,第二个是 456,等等。这可能吗?
您只需要使用 ta.fieldid
而不是 list_of_ids[i]
select ta.fielda,ta.fieldb,
(select sum(tb.field1)-sum(tb.field2) as fieldc
from tableb tb
where tb.fieldid = ta.fieldid )
from tablea ta
where ta.fieldid in (list_of_ids);
试试这个代码:
select ta.fielda,ta.fieldb,
(select sum(field1)-sum(field2) as fieldc
from tableb tb
where tb.fieldid = ta.fieldid )
from tablea ta
where ta.fieldid in (list_of_ids);
子查询锚定到外部查询中的单个记录。
select fielda,fieldb,
(select sum(field1)-sum(field2) as fieldc
from tableb
where tableb.fieldid = tablea.fieldid)
from tablea
where tablea.fieldid in (list_of_ids);
只需使用一个简单的比较运算符将您的子查询与外部查询相关联,您的子查询就会与外部查询找到的任何字段 ID 进行比较。
我正在按照这些思路写一些东西:
select fielda,fieldb,
(select sum(field1)-sum(field2) as fieldc
from tableb
where fieldid = list_of_ids[i])
from tablea
where fieldid in (list_of_ids);
在子查询中,我想使用外部 where 子句中的值。因此,如果 list_of_ids 是 123,456,789,子查询中的 fieldid 将是 123,第二个是 456,等等。这可能吗?
您只需要使用 ta.fieldid
而不是 list_of_ids[i]
select ta.fielda,ta.fieldb,
(select sum(tb.field1)-sum(tb.field2) as fieldc
from tableb tb
where tb.fieldid = ta.fieldid )
from tablea ta
where ta.fieldid in (list_of_ids);
试试这个代码:
select ta.fielda,ta.fieldb,
(select sum(field1)-sum(field2) as fieldc
from tableb tb
where tb.fieldid = ta.fieldid )
from tablea ta
where ta.fieldid in (list_of_ids);
子查询锚定到外部查询中的单个记录。
select fielda,fieldb,
(select sum(field1)-sum(field2) as fieldc
from tableb
where tableb.fieldid = tablea.fieldid)
from tablea
where tablea.fieldid in (list_of_ids);
只需使用一个简单的比较运算符将您的子查询与外部查询相关联,您的子查询就会与外部查询找到的任何字段 ID 进行比较。