HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMinor

Returning information in a nested select in the outermost select

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
theoutermostnestedreturningselectinformation

Problem

I've got the following working,

SELECT info1, info2 
FROM   TableA 
WHERE  id IN (SELECT xid 
              FROM   TableB 
              WHERE  userid=3 AND active=1)


which works fine, but in TableB I also have a team column which I'd like to select and return in the outermost select statement, something like this "pseudo" query, which of course doesn't work:

SELECT info1, info2, team 
FROM   TableA 
WHERE  id IN (SELECT xid, team 
              FROM   TableB 
              WHERE  userid=3 AND active=1)


I had a read around and thought maybe AS could help but I don't really know for sure. Can anybody suggest a solution? Is this even possible?

Solution

This is what JOINs are for:

SELECT  A.info1, 
        A.info2, 
        B.team 
FROM TableA A
INNER JOIN TableB B
    ON A.id = B.xid
WHERE B.userid = 3 
AND B.active=1
;

Code Snippets

SELECT  A.info1, 
        A.info2, 
        B.team 
FROM TableA A
INNER JOIN TableB B
    ON A.id = B.xid
WHERE B.userid = 3 
AND B.active=1
;

Context

StackExchange Database Administrators Q#208170, answer score: 7

Revisions (0)

No revisions yet.