问题
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?
回答1:
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;
回答2:
SELECT
cus.CustomerID,
cus.Name,
cus.Email,
cus.MobileNo,
cus.OtherNo,
bloc.Block,
flor.FlooerNo,
flat.FlateNo
FROM
((Customer cus inner join BuildingBlocks bloc on cus.Block=bloc.BlockId) inner join BuildingFloors flor on cus.Flooler=flor.FlooerID)inner join BuildingFlateNo flat on cus.FlateNo=flat.FlateId
WHERE
cus.Isdeleted=false
来源:https://stackoverflow.com/questions/19121041/ms-access-query-joining-two-tables