zoukankan      html  css  js  c++  java
  • 扩大范围的多条件查询

    代码
    declare @TempTab Table(id int,a varchar(50),b varchar(50),c varchar(100));
    insert into @TempTab(id,a,b,c) values(1,'aaa111','bbb111','ccc111');
    insert into @TempTab(id,a,b,c) values(2,'aaa222','bbb222','ccc222');
    insert into @TempTab(id,a,b,c) values(3,'aaa333','bbb333','ccc333');
    insert into @TempTab(id,a,b,c) values(4,'aaa444','bbb444','ccc444');
    insert into @TempTab(id,a,b,c) values(5,'aaa555','bbb555','ccc555');
    select * from @TempTab;

    declare @q_a varchar(50);
    declare @q_b varchar(50);
    declare @q_c varchar(50);
    set @q_a='aaa111';
    set @q_b='bbb222';
    set @q_c='ccc111';
    --set @q_a='aaa111';
    --
    set @q_b='bbb222';
    --
    set @q_c='ccc333';

    declare @count int
    select @count=count(*from @TempTab where a=@q_a and b=@q_b and c=@q_c
    if @count>0
        
    begin
            
    select * from @TempTab where a=@q_a and b=@q_b and c=@q_c
        
    end
    else
        
    begin
            
    select @count=count(*from @TempTab where (a=@q_a and b=@q_bor (a=@q_a and c=@q_cor (b=@q_b and c=@q_c)
            
    if @count>0
                
    begin
                    
    select * from @TempTab where (a=@q_a and b=@q_bor (a=@q_a and c=@q_cor (b=@q_b and c=@q_c)
                
    end
            
    else
                
    begin
                    
    select @count=count(*from @TempTab where a=@q_a or b=@q_b or c=@q_c
                    
    if @count>0
                        
    begin
                            
    select * from @TempTab where a=@q_a or b=@q_b or c=@q_c
                        
    end
                    
    else
                        
    begin
                            
    select * from @TempTab
                        
    end
                
    end
        
    end
    代码
    declare @orgcity varchar(200);
    declare @dstcity varchar(200);
    declare @FromTime DATETIME;
    declare @flightNo varchar(50);
    declare @AirCompanyCode varchar(50);
    declare @CabinType varchar(50);

    set @orgcity='CAN';
    set @dstcity='XIY';
    set @FromTime='2010-05-08 00:00:00:000';
    set @flightNo='cz3208';
    set @AirCompanyCode='cz';
    set @CabinType='u';


    SELECT * FROM SpecialManager where 
    (
    @FromTime between Orgdate and DstDate and orgcity like '%'+@orgcity+'%' and dstcity like '%'+@dstcity+'%' and specialType=0)

    SELECT  top 1 * FROM SpecialManager where (@FromTime between Orgdate and DstDate and orgcity like '%'+@orgcity+'%' and dstcity like '%'+@dstcity+'%' and specialType=0and (flightNo=@flightNo  OR ISNULL(flightNo,'')=''AND (AirCompanyCode=@AirCompanyCode  OR ISNULL(AirCompanyCode,'')=''AND (CabinType like '%'+@CabinType+'%'  OR ISNULL(CabinType,'')=''ORDER BY AirCompanyCode desc,flightNo desc,CabinType desc
  • 相关阅读:
    19.递归法和非递归法反转链表[ReverseLinkedList]
    18.用两个栈实现队列[2StacksToImplementQueue]
    17.把字符串转换成整数[atoi]
    16.O(logn)求Fibonacci数列[Fibonacci]
    15.含有指针成员的类的拷贝[ClassCopyConstructorWithPointerMember]
    14.约瑟夫环问题[JosephusProblem]
    13.第一个只出现一次的字符[FindFirstNotRepeatingChar]
    12.从上往下遍历二元树[LevelOrderOfBinaryTree]
    洛谷 P2919 [USACO08NOV]守护农场Guarding the Farm
    洛谷 P2733 家的范围 Home on the Range
  • 原文地址:https://www.cnblogs.com/xnxylf/p/1732235.html
Copyright © 2011-2022 走看看