Search code examples
mysqldatabasejoinmysql-5.6

Is This Join Possible?


I have a couple of tables that look like this.

table_a     |   table_b
-------------------------
prim_key    |   prim_key
zero_or_one |   value1
valueA      |   value2
valueB      |   value3
valueZ      |

What I'm hoping to do is retrieve all of the values (prim_key, value1, value2, value3) from TABLE B if the primary keys of each table match and the value of zero_or_one in TABLE A is 0.

I'm completely new to joins, and I'm not exactly sure which join I should be using for this, but it seems like a FULL OUTER JOIN is most appropriate.

SELECT table_b.*
FROM table_a
FULL OUTER JOIN table_b
ON table_a.prim_key = table_b.prim_key

Is this even possible?

Am I using the right join for the job?

Is my "select all" syntax correct?


Solution

  • Since you want entries from table_b only when there is a matching primary key found in the table_a; a simple Inner Join would suffice in this case

    SELECT table_b.*
    FROM table_b
    INNER JOIN table_a
      ON table_a.prim_key = table_b.prim_key AND 
         table_a.zero_or_one = 0