Jamie Moffat
Jamie Moffat

Reputation: 127

Replace ID number in Table A with string from Table B

Table A contains a list of prices for materials, one of the columns contains the material ID.

Table B contains the material ID and the material name as a string.

I'd like to include the material name from Table B where the material ID in table A matches.

I understand this would use some form of join but I'm new to SQL and unfamiliar with the syntax.

something like:

SELECT * 
FROM prices 
    JOIN materials AS material_name ON prices.material = materials.id

I understand the above is incomplete and likely incorrect.

How can I pull in the value of the materials.name column where the prices.material and materials.id column match?

Upvotes: 0

Views: 117

Answers (1)

shubhamr238
shubhamr238

Reputation: 1408

Its better to use left outer join in this type of problems.

SELECT a.material_id,b.material_name,a.price
From A a
LEFT JOIN B b
ON a.material_id=b.material_id;

Now change the columns you need in the SELECT statement according to your requirement.

Upvotes: 2

Related Questions