bfieck bfieck - 4 months ago 9
SQL Question

SQL join columns between two tables that don't have the same columns

So I don't know if it's because I don't know enough terminology or if it hasn't been done, but among the wealth of answers I can't find one that addresses my problem. I have two tables, we'll call them table A and table B. Table A has a foreign key that points to a single row in B. I want to select entries in col1, col2, and col3 of A and add to that selection col4 and col5 from B where the row in A has an id column that points to a row in B. e.g.
enter image description here

I thought this should be easy with an

INNER JOIN
, but I can't write anything that results in anything but a syntax error. I'm pretty new to Sql, but this doesn't seem too difficult. Any help is always appreciated.

NOTE: I'm using Microsoft SQL Server, which I'm told is important to know.

Answer

Use inner join

select a.col1, a.col2, a.col3, b.col1, b.col2
from tableA as a
inner join tableB as b on a.b_id = b.id 
Comments