[英]SQL Query for Inner Join
有三个表主,常规和客户。
我将ControlId保存在Master和Regular的客户master中。 我想从客户记录中获得Master的个人资料。
通过使用下面的查询。 我可以从常规位置获取MasterID,但我想要Profile。
询问
select * from customer where refId='R000003'
(select ControlId from regular where LicenseId='R000003')
结果
主表
常规表
我的查询是..
SELECT Customer.CustomerId, Regular.LicenseId, Regular.ControlId,
Master.FullName, Master.profile
FROM Customer INNER JOIN
Regular ON Customer.RefId = Regular.LicenseId INNER JOIN
Master ON Regular.ControlId = Master.MasterId
WHERE (Customer.RefId = 'R000003')
但是它显示了Regural唯一的我想要大师记录...
你是这个意思吗? 我不确定..
select regular.ControlId, master.profile
from regular r inner join master m ON (r.controlId = m.masterId)
where regular.LicenseId='R000003'
发布数据图像无济于事。 没有人要输入。粘贴示例。
我猜想RefId
和LicenseId
引用。 我认为这是您想要的查询:
select c.*, m.profile
from customer c join
regular r
on c.refId = r.LicenseId join
master m
on r.controlId = m.MasterId;
我建议您修理桌子。 不同表中的联接键应具有相似的名称,因此您知道它们排成一行。 实际上,我几乎总是将联接键命名为“ Id”,因此此查询看起来更像是:
select c.*, m.profile
from customer c join
regular r
on c.CustomerId = r.CustomerId join
master m
on r.MasterId = m.MasterId;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.