开发者

MySQL Query Two Table Join

开发者 https://www.devze.com 2023-03-13 22:12 出处:网络
having a little trouble with this query.I have two tables ... Account - ResourceID (int) AccountID (int) (unique auto-inc)

having a little trouble with this query. I have two tables ...

Account -
ResourceID (int)
AccountID (int) (unique auto-inc)

Resource -
TextName (varchar)
ResourceID (int) (unique auto-inc)
CompanyID (int)

All I have is the AccountID and I Need to make 1 query that will tell me the TextName and ResourceID of all records in the Resource table who have the same CompanyID as the record in the ac开发者_JS百科count table that has the same ResourceID and the AccountID that I provide.

Here is what I have so far and already it has narrowed it down to only one entry ... and I have not even begun to try to incorporate the CompanyID yet.

SELECT r.ResId, r.FirstName, r.LastName
FROM account a, resource r
WHERE a.AccId='7' AND a.ResId = r.ResId 

Any help is much appreciated. Thanks


You need an auto join to get the similar resource sharing the company !

SELECT rSameCompany.ResId, rSameCompany.FirstName, rSameCompany.LastName
FROM resource r
    INNER JOIN resource rSameCompany
        ON r.CompanyID = rSameCompany.CompanyID 
    INNER JOIN account a
        ON r.ResourceID = a.ResourceID 
        AND a.AccId='7' 


You want to LEFT JOIN on Account.ResourceID = Resource.ResourceID.

0

精彩评论

暂无评论...
验证码 换一张
取 消