简体   繁体   English

用于内部联接的SQL查询

[英]SQL Query for Inner Join

There Is three table Master, Regular and Customer. 有三个表主,常规和客户。

I'm saving ControlId in customer master for both Master and Regular. 我将ControlId保存在Master和Regular的客户master中。 I want to get Profile from Master from the Customer Record. 我想从客户记录中获得Master的个人资料。

By using below Query. 通过使用下面的查询。 I'm able to get MasterID from regular But I want Profile. 我可以从常规位置获取MasterID,但我想要Profile。

Query 询问

select * from customer where refId='R000003'
    (select ControlId from regular where LicenseId='R000003')

Result 结果

在此处输入图片说明

Master Table 主表

在此处输入图片说明

Regular Table 常规表

在此处输入图片说明

My Query Is.. 我的查询是..

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') 

But Its showing Regural's only I want Masters record also... 但是它显示了Regural唯一的我想要大师记录...

Is this what you mean? 你是这个意思吗? I'm not sure.. 我不确定..

select regular.ControlId, master.profile
from regular r inner join master m ON (r.controlId = m.masterId)
where regular.LicenseId='R000003'

Posting an image of your data is not helpful. 发布数据图像无济于事。 No one is going to type this in. Paste the sample. 没有人要输入。粘贴示例。

I am going to guess that RefId and LicenseId refer to each other. 我猜想RefIdLicenseId引用。 I think this is the query you want: 我认为这是您想要的查询:

select c.*, m.profile
from customer c join
     regular r
     on c.refId = r.LicenseId join
     master m
     on r.controlId = m.MasterId;

I would advise you to fix your tables. 我建议您修理桌子。 Join keys in different tables should have similar names, so you know they line up. 不同表中的联接键应具有相似的名称,因此您知道它们排成一行。 In fact, I almost always name my join keys as "Id", so this query would look more like like: 实际上,我几乎总是将联接键命名为“ 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM