Skip to content
Advertisement

How to use the results of one query in the next query?

I have read answers here but I am still not quite sure how I would do this regarding two columns of a table and more than one result per query.

So, the first query would look like this in my Node app:

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

I need the result for the following query:

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

I only need the results of the second query but I fail to implement only one query doing both. Can someone help me out? Any help is appreciated.

Thank you!

Advertisement

Answer

You seem to want 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 and @profile_stype are the parameters to the query – that I would recommend using instead of concatenating variables in the query string.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement