0

我有下表,模式是


表:成员

Id        // PK Int Auto Increment
MemberID
MembershipType        // Holds MemberShipMaster types 1/2/3 for Prem, Corp or Free
RegisteredCountry     // FK of CountryMaster table
RegisteredState       // FK of States Table
RegisteredCity        // FK of Districts table
PostalCountry         // FK of CountryMaster table
PostalState           // FK of States Table
PostalCity            // FK of Districts table

请注意:邮政地址和注册地址可能不同(其逻辑...)


表 : CountryMaster

Id        // PK , int auto increment
Country   // Name of Country

表 : MemberShipMaster

Id               // PK int auto increment
Type             // Type Id int   , 1,2,3 respectively for Prem, Corp and Free
TypeName          // Membership Type Name e.g. Premium, Corporate, Free

Tbale : 城市

Id,         // PK int auto increment
Name       
StateId     // FK, PK of states table

表:状态

Id   // PK int auto increment
Name           
CountryId // FK , PK of COuntry

以下是我的查询

SELECT Id , MemberID, MembershipType, Districts.Name as RegisteredCity,
States.Name as RegisteredState,CountryMaster.Country as RegisteredCountry,
Districts.Name as PostalCity,States.Name as PostalState, CountryMaster.Country as PostalCountry 
FROM   dbo.JFPMembers RIGHT  JOIN MemberShipMaster ON MemberShipMaster.Id= dbo.JFPMembers.MembershipType  
LEFT  JOIN  Districts ON dbo.JFPMembers.PostalCity = Districts.Id OR  Districts.Id =JFPMembers.RegisteredCity
LEFT  JOIN  States ON States.Id = dbo.JFPMembers.RegisteredState OR States.Id = dbo.JFPMembers.PostalState
LEFT  JOIN CountryMaster ON CountryMaster.Id = dbo.JFPMembers.RegisteredCountry and CountryMaster.Id = dbo.JFPMembers.postalCountry
Where Id= @id

我的预期输出:

根据列中保存的 id,在下面的字段中输入适当的城市、州或国家/地区名称。但相反,我在所需的列中得到空值。我已经确认这两个值存在于数据库中以用于连接。

Districts.Name 作为 RegisteredCity,States.Name 作为 RegisteredState,CountryMaster.Country 作为 RegisteredCountry,Districts.Name 作为 PostalCity,States.Name 作为 PostalState,CountryMaster.Country 作为 PostalCountry

4

3 回答 3

1

Postal needs its joins to the respective country, state and city tables on its own via separate aliases... Same with Registered...

SELECT 
      M.Id, 
      M.MemberID, 
      M.MembershipType, 
      MM.TypeName as MembershipTypeName,
      RegCountryAlias.Country as RegisteredCountry,
      RegStatesAlias.Name as RegisteredState,
      RegCitiesAlias.Name as RegisteredCity,
      PostalCountryAlias.Country as PostalCountry,
      PostalStatesAlias.Name as PostalState,
      PostalCitiesAlias.Name as PostalCity
   FROM   
      JFPMembers M
         JOIN MemberShipMaster MM
            ON M.MembershipType = MemberShipMaster.Id

         JOIN CountryMaster as RegCountryAlias
            ON M.RegisteredCountry = RegCountryAlias.ID
         JOIN States as RegStatesAlias
            ON M.RegisteredState = RegStatesAlias.ID
         JOIN Cities as RegCitiesAlias
            ON M.RegisteredCity = RegCitiesAlias.ID

         JOIN CountryMaster as PostalCountryAlias
            ON M.PostalCountry = PostalCountryAlias.ID
         JOIN States as PostalStatesAlias
            ON M.PostalState = PostalStatesAlias.ID
         JOIN Cities as PostalCitiesAlias
            ON M.PostalCity = PostalCitiesAlias.ID
   WHERE
      M.ID = @id

As Kenneth pointed out about possible NULLs if any column didn't specifically find a match in the corresponding joined table, it would not return a record. Therefore, you would probably change the JOINs to LEFT JOINs. Additionally, you mentioned a "Districts" table, but did not provide, but DID provide your "Cities" table which is what I used as the basis of the joins.

于 2013-06-27T18:48:19.287 回答
1

你的问题是你试图加入两个不同的条件。而是加入您的查找表两次。像这样:

SELECT Id , MemberID, MembershipType, RegDistricts.Name as RegisteredCity,
    RegStates.Name as RegisteredState, RegCountryMaster.Country as RegisteredCountry,
    PostalDistricts.Name as PostalCity, PostalStates.Name as PostalState, 
    PostalCountryMaster.Country as PostalCountry 
FROM dbo.JFPMembers 
LEFT JOIN MemberShipMaster 
    ON MemberShipMaster.Id = dbo.JFPMembers.MembershipType  
LEFT JOIN Districts RegDistricts
    ON RegDistricts.Id = JFPMembers.RegisteredCity
LEFT JOIN Districts  PostalDistricts
    ON dbo.JFPMembers.PostalCity = PostalDistricts.Id 
LEFT JOIN States RegStates
    ON RegStates.Id = dbo.JFPMembers.RegisteredState 
LEFT JOIN States PostalStates
    ON PostalStates.Id = dbo.JFPMembers.PostalState
LEFT JOIN CountryMaster RegCountryMaster
    ON RegCountryMaster.Id = dbo.JFPMembers.RegisteredCountry 
LEFT JOIN CountryMaster PostalCountryMaster
    ON PostalCountryMaster.Id = dbo.JFPMembers.postalCountry
Where Id= @id
于 2013-06-27T18:40:17.510 回答
0

左加入 CountryMaster ON CountryMaster.Id = dbo.JFPMembers.RegisteredCountry 和 CountryMaster.Id = dbo.JFPMembers.postalCountry

你是说and还是or

于 2012-09-07T10:51:58.970 回答