如何查看基于主键的唯一一条记录?

时间:2017-10-25 09:06:13

标签: sql sql-server asp.net-mvc sql-server-2008

注册表我插入一条DoctorID的记录为SELECT TOP 1000 [DoctorID] ,[DoctorSource] ,[Tittle] ,[DoctorName] ,[DoctorCode] ,[RegistrationNo] ,[Gender] ,[DocDOB] ,[DocAddress1] ,[DocAddress2] ,[Country] ,[State] ,[City] ,[Pincode] ,[CountryCode] ,[Mobile] ,[Phone] ,[Email] ,[Website] ,[AadhaarCardNo] ,[Language] ,[AboutDoctor] ,[Status] ,[IPAddress] ,[Latitude] ,[Longitude] ,[CreatedBy] ,[CreatedDate] ,[ModifiedBy] ,[ModifiedDate] FROM [VAN_DOCTOR].[dbo].[DoctorRegistration]

DoctorID

Registration

根据ExperienceID我们在体验表中插入了两条记录,其中主键为DoctorID参考键为SELECT TOP 1000 [ExperienceID] ,[DoctorID] ,[HospitalName] ,[Department] ,[Designation] ,[FromDate] ,[ToDate] ,[WorkDescription] ,[CreatedBy] ,[CreatedDate] ,[ModifiedBy] ,[ModifiedDate] FROM [VAN_DOCTOR].[dbo].[Doctorexperience]

doctorID

Experience

在我的视图页面中显示了两条记录,但我们只有一条USE [VAN_DOCTOR] GO /****** Object: StoredProcedure [dbo].[GetFullDoctorMEDEIL] Script Date: 25-Oct-17 2:18:08 PM ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER Procedure [dbo].[GetFullDoctorMEDEIL] as begin select DR.[DoctorID], DR.[DoctorSource], DR.[Tittle], DR.[DoctorName], DR.[DoctorCode], DR.[RegistrationNo], DR.[Gender], DR.[DocDOB], DR.[DocAddress1], DR.[DocAddress2], C.CountryName, S.StateName, DR.[City], DR.[Pincode], C.CountryCode + C.DialingCode As Countrydial, DR.[Mobile], DR.[Phone], DR.[Email], DR.[Website], DR.[AadhaarCardNo], DR.[Language], DR.[AboutDoctor], DR.[Status], DEX.[ExperienceID], DEX.[HospitalName], DEX.[Department], DEX.[Designation], DEX.[FromDate], DEX.[ToDate], DEX.[WorkDescription], DE.[DoctorEducationID], DE.[Qualification], DE.[Major], DE.[YearOfPassing], DE.[Institute], DC.[DoctorinformationID], DC.[CHospitalName], DC.[Address1], DC.[Address2], CD.CountryName, SD.StateName, DC.[CCity], DC.[CPincode], DC.[DoctorRoomNo], DC.[Consultingfees], SY.SpecialityName, CY.SpecialistName, DB.[BusinessDaysID], DB.[Sunday], DB.[Monday], DB.[Tuesday], DB.[Wednesday], DB.[Thursday], DB.[Friday], DB.[Saturday], DB.[SunStartTime], DB.[SunEndTime], DB.[MonStartTime], DB.[MonEndTime], DB.[TueStartTime], DB.[TueEndTime], DB.[WedStartTime], DB.[WedEndTime], DB.[ThuStartTime], DB.[ThuEndTime], DB.[FriStartTime], DB.[FriEndTime], DB.[SatStartTime], DB.[SatEndTime], DS.[DoctorSocialID], DS.[FaceBookID], DS.[TwitterID], DS.[linkedinID], DS.[PinterestID], DS.[GooglePlusID], DR.[CreatedDate] from DoctorRegistration AS DR LEFT JOIN [VAN_SETTING].[dbo].[Country] C ON C.CountryID=DR.Country LEFT JOIN [VAN_SETTING].[dbo].[State] S ON S.StateID=DR.State LEFT JOIN [dbo].[Doctorexperience] as DEX ON DEX.DoctorID=DR.DoctorID LEFT JOIN [dbo].[DoctorEducation] as DE ON DE.DoctorID=DR.DoctorID LEFT JOIN [dbo].[DoctorclinicInformation] as DC ON DC.DoctorID=DR.DoctorID LEFT JOIN [VAN_SETTING].[dbo].[Country] CD ON CD.CountryID=DC.CCountry LEFT JOIN [VAN_SETTING].[dbo].[State] SD ON SD.StateID=DC.CState LEFT JOIN [VAN_SETTING].[dbo].[Specialist] CY ON CY.SpecialistID=DC.SpecialistID LEFT JOIN [VAN_SETTING].[dbo].[Speciality] SY ON SY.SpecialityID=DC.SpecialityID LEFT JOIN [dbo].[ClinicBusinessHour] as DB ON DB.DoctorID=DR.DoctorID LEFT JOIN [dbo].[Doctorsocialnetwork] as DS ON DS.DoctorID=DR.DoctorID End 所以

我只需要查看一条记录如何解决这个问题

{{1}}

View

2 个答案:

答案 0 :(得分:1)

这导致您有多个记录具有相同的DoctorId,如果我看到最终查询结果为JOIN,则两个结果记录都相同,在这种情况下您可以使用{ {1}}喜欢

distinct

您也可以使用ROW_NUMBER()分析函数从这些重复项中只获取一条记录

   select distinct
        DR.[DoctorID],
        DR.[DoctorSource],
        DR.[Tittle],
        DR.[DoctorName],

答案 1 :(得分:0)

如果您加入Doctorexperience,您将获得与此表中一样多的记录。如果DoctorRegistration中有一条记录,Doctorexperience中有5条记录,则会加入5条记录。不同之处将无济于事,因为您从Doctorexperience获得了ExperienceID专栏,这是主键。也许你只需要Doctorexperience的第一个记录,这样你就可以做那样的事情,而不是加入Doctorexperience:

OUTER APPLY 
(
  select top 1 DEX.ExperienceID, ... from [dbo].[Doctorexperience]  DEX
   where DEX.DoctorID=DR.DoctorID
) EXTOP