我有下表,模式是
表:成员
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