简体   繁体   中英

Inner Join Two Column From Another Table with Custom Column Name

I have two table called tbl_project_requirements and tbl_units. I want select all from tbl_project_requirements and want join id as unit_id and name as unit_name from tbl_units. My current query is like below

SELECT * FROM `tbl_requirements` INNER JOIN tbl_units AS t2 WHERE unit_type = t2.id 

Its working fine but giving me id as id and name as name so there multiple column with same column name in my result like below

在此处输入图片说明

Let me know if someone can help me for solve the puzzle. Thanks

Use alias for 2nd table's id and Name column

SELECT t1.*, t2.id as t2id, t2.name as t2Name
FROM `tbl_requirements` t1 INNER JOIN tbl_units AS t2 
on unit_type = t2.id

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