首页 > 解决方案 > 如何在下一个查询中使用一个查询的结果?

问题描述

我在这里阅读了答案,但我仍然不太确定如何针对表的两列和每个查询的多个结果执行此操作。

因此,第一个查询在我的 Node 应用程序中如下所示:

select stype, lid from Profiles where lid_P=${profile.lid} and stype_P='${profile.stype}'; 
// result can consist of 1-50 objects

我需要以下查询的结果:

select * from an where stype=stype and lid=lid;
// where lid and stype are results from the other query

我只需要第二个查询的结果,但我没有实现只执行两个查询的一个查询。有人可以帮我吗?任何帮助表示赞赏。

谢谢!

标签: sqlnode.jssql-serversubquerycorrelated-subquery

解决方案


你似乎想要exists

select *
from an a
where exists(
    select 1
    from profiles p
    where 
        p.stype = a.stype and p.lid = s.lid
        and p.lid_p = @profile_lid and p.style_p = @profile_stype
)

@profile_lid并且@profile_stype是查询的参数 - 我建议在查询字符串中使用而不是连接变量。


推荐阅读