采购价格表tblPur,表中两个字段fCName,fPrice
查询字段fCName中包含字符串@A,@B,@C的最大价格,求高效查询语句,字段fCName已经建立索引。

解决方案 »

  1.   

    select *  from  tblPur where charindex('@a',fCName)>0
    union all
    select *  from  tblPur where charindex('@b',fCName)>0
    union all
    select *  from  tblPur where charindex('@c',fCName)>0
      

  2.   

    把* 替换掉,会在高点,其它高效的想不出来了select fCName,fPrice   from  tblPur where charindex('@a',fCName)>0 
    union all 
    select fCName,fPrice   from  tblPur where charindex('@b',fCName)>0 
    union all 
    select fCName,fPrice   from  tblPur where charindex('@c',fCName)>0
      

  3.   

    可能让大家误解了,是@A @B @C全部包含的最大价格。
      

  4.   

    select
     * 
    from
     tblPur t 
    where 
     not exists(select 1 from tb where fCName=t.fCName and fPrice>t.fPrice) and fCName in (@A,@B,@C)
      

  5.   

    SELECT MAX(PRICE)PRICE FROM 
    (select *  from  tblPur where charindex('@a',fCName)>0 
    union all 
    select *  from  tblPur where charindex('@b',fCName)>0 
    union all 
    select *  from  tblPur where charindex('@c',fCName)>0)AS T
      

  6.   

    select Max(case when charindex(@A,fCName)>0 and charindex(@B,fCName)>0 and charindex(@C,fCName)>0  then fPrice else 0 end ) from tblPur 现在使用以上语句,还是太慢,请各位帮帮忙。
      

  7.   


    --使用到charindex , like的话,索引基本无效.
    select max(fPrice) from tblPur where charindex('@a',fCName ) > 0 or charindex('@b',fCName ) or charindex('@c',fCName )select max(fPrice) from tblPur where fCName like '%@a%' or fCName like '%@b%' or fCName like '%@c%'
      

  8.   

    乌龟大侠OR的效率比UNION ALL要低点吧,不过索引都没效了
      

  9.   

    select  max(price) from tb where fCName like '%@a%@b%@c%'
      

  10.   

    大家帮帮忙啊,tblPur只有2万条数据,查询大约100次要13秒左右,太慢了啊
      

  11.   

    学习 我以为 charindex 会用到索引的 呵呵
      

  12.   

    这个靠SQL语句来优化的空间不大了。。
      

  13.   

    select max(fPrice)
     from tblPur
    where charindex(@A,fCName)>0 
      and charindex(@B,fCName)>0 
      and charindex(@C,fCName)>0;
      

  14.   


    --11楼这样是不对的:@a、@b、@c没有顺序之分!