java threetable,Three table join in sql

可以将文章内容翻译成中文,广告屏蔽插件会导致该功能失效:

问题:

I'm trying to join three tables.

Tables:

HumanResources.Employees: Employee_ID(Primary Key), First_Name, Title -- also known as Employee_Title,

ProjectDetails.TimeCards: Employee_ID(Foreignkey), Project_ID (Foreign Key)

ProjectDetails.Projects: Project_Name, Project_ID(Primary Key)

I tried joining them using a temporary table.

select b.First_Name, b.Title, c.Project_ID from HumanResources.Employees b -- select statement

inner join ProjectDetails.TimeCards c on b.Employee_ID = c.Employee_ID -- first join seems to be the only one working.

inner join (select d.Project_Name as Project_Name, Project_ID from ProjectDetails.Projects d) as d on d.Project_ID=c.Project_ID -- second join doesn't seem to work.

回答1:

The use of the subquery is redundunt at best, and also the common alias of "d" may be a source of error.

Just do:

select b.First_Name, b.Title, c.Project_ID

from HumanResources.Employees b

inner join ProjectDetails.TimeCards c on b.Employee_ID = c.Employee_ID

inner join ProjectDetails.Projects d on d.Project_ID=c.Project_ID

回答2:

In the inner query for Project_ID use column alias name and then join on the alias column name.also try to have a different alias name for the sub query.

回答3:

I don't see the benefit of using a subquery in your query. Could you try the below query?

Make sure in your table Projects has matching Project_ID otherwise of course nothing would come up.

SELECT b.First_Name

,b.Title

,c.Project_ID

FROM HumanResources.Employees b

INNER JOIN ProjectDetails.TimeCards c ON b.Employee_ID = c.Employee_ID

INNER JOIN ProjectDetails.Projects d ON d.Project_ID = c.Project_ID

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值