简体   繁体   中英

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?

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;
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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM