SQL JOIN two tables with two equally named columns
I have two tables that I'd like to join
Right now I just do :
SELECT * FROM (default_insurance)
JOIN default_profiles ON uid = default_profiles.id
WHERE `uid` = '1
problem is that both default_insurance
and default_profiles
contains a column开发者_运维问答 named company
, and I only want the one from default_insurance
, but is there a way to make a join that will automatically prefer columns from one of the tables WITHOUT having to SELECT (all columns that I want)
You can always specify what exactly do you need (+use table aliases):
SELECT t1.*, t2.company AS default_insurance_company
FROM default_profiles t1 LEFT JOIN default_insurance t2
ON t1.uid = t2.id
WHERE t1.uid = 1
(MySQL example)
Above will return all columns from t1 and additionally column company
from t2, but on your result it will be named default_insurance_company
.
Automatically no. You need to use like this:
SELECT [all columns that you want], di.company
FROM default_insurance di
JOIN default_profiles dp ON di.uid = dp.id
WHERE di.uid = '1
精彩评论