[英]How to join 3 tables in SQL Server?
I have 3 tables : 我有3张桌子:
I want to get only the name of the services, based on the ID_Transaction
in the Transaction
table. 我想根据
Transaction
表中的ID_Transaction
获取服务的名称。
The table and output that I want: 我想要的表和输出:
Code I tried: 我试过的代码:
Select se.Name
from Transactions as td, Services as se
where td.ID_Transaction = 'TRA1'
But the output is displaying all the name from service table 但输出显示服务表中的所有名称
select c.name
from Transaction as a
inner join Detail as b
on a.ID_Transaction = b.ID_Transaction
inner join Services as c
on b.id_services = c.id_services;
Please try the following query: 请尝试以下查询:
select *
from Transaction,Detail,Services
where Transaction.ID_Transcation=Detail.ID_Transaction
and Services.ID_Services=Detail.ID_Services;
You are missing Join condition and Table Join . 您缺少加入条件和表加入。
Non ANSI: 非ANSI:
SELECT se.NAME
FROM Transactions AS td
,Transaction_Detail det
,Services AS se
WHERE
td.ID_Transaction = det.ID_Transaction
AND det. ID_Services= se. ID_Services
AND td.ID_Transaction = 'TRA1'
ANSI: ANSI:
SELECT se.NAME
FROM Transactions AS td
INNER JOIN Transaction_Detail AS det ON td.ID_Transaction = det.ID_Transaction
INNER JOIN Services AS se ON det. ID_Services= se. ID_Services
WHERE td.ID_Transaction = 'TRA1'
ANSI standard Joins are preferred.
ANSI标准连接是首选。 They are readable and accepted over multiple RDBMS.
它们可通过多个RDBMS读取和接受。 If you use NON ANSI Join and if you forgot join condition then the result > will be
CROSS JOIN
如果您使用NON ANSI Join并且忘记了连接条件,那么结果>将是
CROSS JOIN
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.