ms access query joining two tables

♀尐吖头ヾ 提交于 2019-12-08 04:13:23

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!