3

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?

8

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
;
5

You can JOIN both tables in this way:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.