skip to Main Content

I am trying to narrow down the results returned from a JOIN so that the shared value equal a specific value.

The basic query which returns the entire contents of the tables.

SELECT t1.c2, t2.c3 FROM t1
INNER JOIN t2 ON t1.c3 = t2.c4

I know I can add t1.c3 to the select list and loop through the returned data to find the t1.c3 with a specific value. But I am wondering if there is an automagical way to get only the joined data when the value of t1.c3 and t2.c4 = ‘xyz’.

2

Answers


  1. Just put your criteria into the join condition:

    SELECT t1.c2, t2.c3 FROM t1
    INNER JOIN t2 ON t1.c3="xyz" and t2.c4="xyz"
    
    Login or Signup to reply.
  2. It seems you are simply asking for a condition in a WHERE clause:

    SELECT t1.c2, t2.c3
    FROM t1
    INNER JOIN t2 ON t1.c3 = t2.c4
    WHERE t1.c3 = 'xyz';
    

    As it is the same column on which you want to filter and join both tables, you can also consider this a cross join on the filtered sets:

    SELECT q1.c2, q2.c3
    FROM       (SELECT * FROM t1 WHERE c3 = 'xyz') q1
    CROSS JOIN (SELECT * FROM t2 WHERE c4 = 'xyz') q2;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search