0

我有一个 SQL 查询,我试图在合并两个查询后获得不同的值。我已经在两个查询中使用了 Distinct。

RegID在表中作为主键,Registration对于注册表中的每个条目,在即表中都有相应的条目RoomType作为RoomTypeRegID键。

RoomType现在的问题是,如果表中有多个条目,RegID我会得到重复的 Regid。

我的查询打印是这样的:

SELECT DISTINCT 
    dbo.tbRegistration.RegID, 
    dbo.tbRegistration.PropertyName, dbo.tbRegistration.IsActive, 
    dbo.tbRegistration.Stars, dbo.tbRegistration.City, 
    dbo.tbRegistration.Address, 
    dbo.tbHotelImages.MainImage, dbo.tbHotelImages.Img1, 
    dbo.tbHotelImages.Img2, dbo.tbHotelImages.Img3, 
    dbo.tbHotelImages.Img4, dbo.tbHotelImages.Img5, 
    dbo.tbHotelImages.Img6, dbo.tbHotelImages.Img7, 
    dbo.tbHotelImages.Img8, dbo.tbHotelImages.Img9, 
    dbo.tbRegistration.PropertyID, 
    dbo.tbHotelFeatures.*, 
    dbo.tbBillingPlan.BaseRate, dbo.tbRoomType.Name as RoomTypeName
FROM         
    dbo.tbRoomType 
LEFT OUTER JOIN
    dbo.tbRegistration ON dbo.tbRoomType.RegID = dbo.tbRegistration.RegID  
LEFT OUTER JOIN
    dbo.tbBillingPlan ON dbo.tbRoomType.RTID = dbo.tbBillingPlan.RTID    
LEFT OUTER JOIN
    dbo.tbHotelFeatures ON dbo.tbRegistration.RegID = dbo.tbHotelFeatures.RegID  
LEFT OUTER JOIN
    dbo.tbHotelImages ON dbo.tbRegistration.RegID = dbo.tbHotelImages.RegID                                          
WHERE 
    tbRegistration.PMS = 0 
    AND tbRegistration.Stars IN (0) OR 1=1 
    AND (tbRegistration.City LIKE '%' + @SearchText + '%')  
    AND (tbRoomType.Adults >= @Adults OR tbRoomType.Children >= @Children)
    AND (dbo.GetFreeRoomType(dbo.tbRoomType.RTID, @CheckIn, @CheckOut) > 0) 
    AND dbo.tbRegistration.IsActive = 1     

UNION

SELECT DISTINCT 
    dbo.tbRegistration.RegID  , dbo.tbRegistration.PropertyName, dbo.tbRegistration.IsActive, dbo.tbRegistration.Stars, dbo.tbRegistration.City, dbo.tbRegistration.Address, 
                      dbo.tbHotelImages.MainImage, dbo.tbHotelImages.Img1, dbo.tbHotelImages.Img2, dbo.tbHotelImages.Img3, dbo.tbHotelImages.Img4, dbo.tbHotelImages.Img5, 
                      dbo.tbHotelImages.Img6, dbo.tbHotelImages.Img7, dbo.tbHotelImages.Img8, dbo.tbHotelImages.Img9, dbo.tbRegistration.PropertyID    , 
                      dbo.tbHotelFeatures.*, dbo.tbBillingPlan.BaseRate,dbo.tbRoomType.Name as RoomTypeName
FROM         dbo.tbRoomType left OUTER JOIN
                      dbo.tbRegistration ON dbo.tbRoomType.RegID = dbo.tbRegistration.RegID  left OUTER JOIN
                      dbo.tbBillingPlan ON dbo.tbRoomType.RTID = dbo.tbBillingPlan.RTID    LEFT OUTER JOIN
                      dbo.tbHotelFeatures ON dbo.tbRegistration.RegID = dbo.tbHotelFeatures.RegID  LEFT OUTER JOIN
                      dbo.tbHotelImages ON dbo.tbRegistration.RegID = dbo.tbHotelImages.RegID                                           
       Where tbRegistration.PMS=1  and   tbRegistration.Stars IN (   0 ) or 1=1 and    (tbRegistration.City like  '%' + @SearchText + '%' )    

and (tbRoomType.Adults>=@Adults  or tbRoomType.Children>=@Children )
 and(dbTestSparrowPMS . dbo.GetFreeRoomType(dbo.tbRoomType.ID, @CheckIn, @CheckOut) >0) and dbo.tbRegistration.IsActive=1



   order by  tbRegistration.RegID

我的程序是这样的:

USE [dbTestSearchMyHotel]
GO
/****** Object:  StoredProcedure [dbo].[prRegistrationSearchFreeHotels]    Script Date: 04/01/2013 12:06:26 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:      <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[prRegistrationSearchFreeHotels]   -- 's','4/27/2013','4/28/2013',1,1,0,0,0,0,0,0,0

    -- Add the parameters for the stored procedure here select * from tbRegistration where Stars in(1,4)

@SearchText varchar(4000),
@CheckIn date,
@CheckOut date,
@Adults int,
@Children int,
@Min float,
@Max float,
@OneStar bit,
@TwoStar bit,
@ThreeStar bit,
@FourStar bit,
@FiveStar bit

    AS
     Declare @SQLQuery AS NVarchar(4000)
Declare @ParamDefinition AS NVarchar(4000) 
declare @n as bit
set @n=0
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here

  Set @SQLQuery='SELECT DISTINCT 
                      dbo.tbRegistration.RegID  , dbo.tbRegistration.PropertyName,dbo.tbRegistration.IsActive, dbo.tbRegistration.Stars, dbo.tbRegistration.City, dbo.tbRegistration.Address, 
                      dbo.tbHotelImages.MainImage, dbo.tbHotelImages.Img1, dbo.tbHotelImages.Img2, dbo.tbHotelImages.Img3, dbo.tbHotelImages.Img4, dbo.tbHotelImages.Img5, 
                      dbo.tbHotelImages.Img6, dbo.tbHotelImages.Img7, dbo.tbHotelImages.Img8, dbo.tbHotelImages.Img9, dbo.tbRegistration.PropertyID    , 
                      dbo.tbHotelFeatures.*, dbo.tbBillingPlan.BaseRate,dbo.tbRoomType.Name as RoomTypeName
FROM         dbo.tbRoomType left OUTER JOIN
                      dbo.tbRegistration ON dbo.tbRoomType.RegID = dbo.tbRegistration.RegID  left OUTER JOIN
                      dbo.tbBillingPlan ON dbo.tbRoomType.RTID = dbo.tbBillingPlan.RTID    LEFT OUTER JOIN
                      dbo.tbHotelFeatures ON dbo.tbRegistration.RegID = dbo.tbHotelFeatures.RegID  LEFT OUTER JOIN
                      dbo.tbHotelImages ON dbo.tbRegistration.RegID = dbo.tbHotelImages.RegID                                          
                              Where  tbRegistration.PMS=0 and  tbRegistration.Stars IN (   ' 
     if @OneStar =1 
     begin
    set @n =1
    set @SQLQuery =@SQLQuery +'  1  '
    end

  if @TwoStar =1 
  begin
     if @n =1
     begin
     set @SQLQuery =@SQLQuery +' , 2 '
       end
     else
     begin
     set @n=1
     set @SQLQuery =@SQLQuery +' 2 '
      end
 end

  if @ThreeStar =1 
  begin
     if @n =1
     begin
    set @SQLQuery =@SQLQuery +' , 3 '
    end
    else
     begin
     set @n=1
     set @SQLQuery =@SQLQuery +' 3  '
    end
 end

  if @FourStar =1 
  begin
     if @n =1
     begin
    set @SQLQuery =@SQLQuery +' , 4 '
    end
    else
    begin
     set @n=1
      set @SQLQuery =@SQLQuery +' 4 '
    end
    end

  if @FiveStar =1 
  begin
    if @n =1
     begin
     set @SQLQuery =@SQLQuery +' , 5 '
    end
     else
     begin
     set @n=1
     set @SQLQuery =@SQLQuery +' 5 '
    end
    end                         
   if @n=0
 begin
  set @SQLQuery =@SQLQuery +' 0 ) or 1=1 and '
 end      
 else
 begin                      
 set @SQLQuery =@SQLQuery +' )    and ' 
 end
 set @SQLQuery =@SQLQuery +'    (tbRegistration.City like  ''%'' + @SearchText + ''%'' )  

 and (tbRoomType.Adults>=@Adults  or tbRoomType.Children>=@Children )
 and( dbo.GetFreeRoomType(dbo.tbRoomType.RTID, @CheckIn, @CheckOut) >0) and dbo.tbRegistration.IsActive=1    '  
 if @Min > 0 and @Max > 0   
 set @SQLQuery =@SQLQuery +' and (tbBillingPlan.BaseRate>=@Min) and  (tbBillingPlan.BaseRate<=@Max)  '





  set @SQLQuery =@SQLQuery +' union  '
  set @n=0
Set @SQLQuery=@SQLQuery+'SELECT DISTINCT 
                      dbo.tbRegistration.RegID  , dbo.tbRegistration.PropertyName, dbo.tbRegistration.IsActive, dbo.tbRegistration.Stars, dbo.tbRegistration.City, dbo.tbRegistration.Address, 
                      dbo.tbHotelImages.MainImage, dbo.tbHotelImages.Img1, dbo.tbHotelImages.Img2, dbo.tbHotelImages.Img3, dbo.tbHotelImages.Img4, dbo.tbHotelImages.Img5, 
                      dbo.tbHotelImages.Img6, dbo.tbHotelImages.Img7, dbo.tbHotelImages.Img8, dbo.tbHotelImages.Img9, dbo.tbRegistration.PropertyID    , 
                      dbo.tbHotelFeatures.*, dbo.tbBillingPlan.BaseRate,dbo.tbRoomType.Name as RoomTypeName
FROM         dbo.tbRoomType left OUTER JOIN
                      dbo.tbRegistration ON dbo.tbRoomType.RegID = dbo.tbRegistration.RegID  left OUTER JOIN
                      dbo.tbBillingPlan ON dbo.tbRoomType.RTID = dbo.tbBillingPlan.RTID    LEFT OUTER JOIN
                      dbo.tbHotelFeatures ON dbo.tbRegistration.RegID = dbo.tbHotelFeatures.RegID  LEFT OUTER JOIN
                      dbo.tbHotelImages ON dbo.tbRegistration.RegID = dbo.tbHotelImages.RegID                                           
       Where tbRegistration.PMS=1  and   tbRegistration.Stars IN (  ' 
     if @OneStar =1 
     begin
    set @n =1
 set @SQLQuery =@SQLQuery +'  1  '
 end
     if @TwoStar =1 
     begin
         if @n =1
        begin
        set @SQLQuery =@SQLQuery +', 2 '
        end
        else
        begin
         set @SQLQuery =@SQLQuery +' 2 '
         set @n=1
            end
     end
  if @ThreeStar =1 
  begin
         if @n =1
          begin
        set @SQLQuery =@SQLQuery +' , 3 '
         end
        else
         begin
         set @n=1
        set @SQLQuery =@SQLQuery +' 3'
            end
 end

  if @FourStar =1 
  begin
         if @n =1
         begin
        set @SQLQuery =@SQLQuery +' , 4 '
        end
        else
        begin
        Set @n=1
        set @SQLQuery =@SQLQuery +' 4 '
        end
 end
   if @FiveStar =1 
   begin
         if @n =1
         begin
        set @SQLQuery =@SQLQuery +', 5 '
        end
        else
        begin
        Set @n=1
         set @SQLQuery =@SQLQuery +' 5 '
        end
 end                         
  if @n=0
 begin
  set @SQLQuery =@SQLQuery +' 0 ) or 1=1 and '
 end      
 else
 begin                      
 set @SQLQuery =@SQLQuery +' )    and ' 
 end
 set @SQLQuery =@SQLQuery +'   (tbRegistration.City like  ''%'' + @SearchText + ''%'' )    

and (tbRoomType.Adults>=@Adults  or tbRoomType.Children>=@Children )
 and(dbTestSparrowPMS . dbo.GetFreeRoomType(dbo.tbRoomType.ID, @CheckIn, @CheckOut) >0) and dbo.tbRegistration.IsActive=1



  '  

 if @Min > 0 and @Max > 0   
 set @SQLQuery =@SQLQuery +' and (tbBillingPlan.BaseRate>=@Min) and  (tbBillingPlan.BaseRate<=@Max) '



 set @SQLQuery=@SQLQuery + ' order by  tbRegistration.RegID'
  --

       Set @ParamDefinition = ' 
@SearchText varchar(4000),
@CheckIn date,
@CheckOut date,
@Adults int,
@Children int,
@Min float,
@Max float,
@OneStar bit,
@TwoStar bit,
@ThreeStar bit,
@FourStar bit,
@FiveStar bit'
   --print @SQLQuery           
    /* Execute the Transact-SQL String with all parameter value's 
       Using sp_executesql Command */
    Execute sp_Executesql     @SQLQuery, 
                @ParamDefinition, 

               @SearchText ,
               @CheckIn ,
               @CheckOut ,
               @Adults ,
               @Children ,
               @Min,
               @Max,
               @OneStar ,
               @TwoStar ,
               @ThreeStar ,
               @FourStar ,
               @FiveStar 



    return



  end
4

1 回答 1

0

UNION 删除重复记录 (UNION = SELECT DISTINCT),而 UNION ALL 返回重复记录。请检查链接。

现在在您的查询中,您正在对查询执行 SELECT DISTINCT 并对其使用 UNION。但是,如果您的两个查询为其中一列返回不同的值,那么您将同时获得它们,并且 UNION 不会删除重复项。

例如

ID 文本

1个

1乙

因此,您要么必须删除具有不同值的列,然后尽可能将重复记录返回给您。或添加过滤掉第一个查询中已选择的记录的条件。

于 2013-04-01T07:18:07.897 回答