CodeMed CodeMed - 3 months ago 7
SQL Question

ms access query joining two tables

I have two tables that need to be output into an un-editable form. I need the syntax for the query.

TableOne has fields id, customer_id, date, type_id.
TableTwo has fields type_id, type_name.


I have:

SELECT * FROM TableOne WHERE customer_id=someVariable


But it just outputs a number for type_id. I need it to print out the type_name associated with the type_id instead of printing out the number. How do I change the syntax of the SQL to get it to do this?

Answer

just use LEFT JOIN or INNER JOIN

LEFT JOIN will give you all t1 even if you have no corresponding type_id in t2 INNER JOIN will only retrieve results where corresponding type_id exists in t1 and t2.

select t1.Id, t1.customer_id, t1.date, t2.type_name
FROM TableOne t1
LEFT JOIN TableTwo t2 on t1.type_id = t2.type_id;