简体   繁体   中英

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. I want to get Profile from Master from the Customer Record.

By using below Query. I'm able to get MasterID from regular But I want 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...

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. 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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