FieldInit表
ValueInit ValueMem
☆ =0 And 0=
★ >=1 And 5>=
★★ >5 And 8>=
★★★ >8 And 20>=
★★★★ >20 And 999999999>=GoodsInfo表
GoodsID GoodsLibNumber
50 8
51 5
52 200
53 0
54 20生成结果如下,求SQL语句,谢谢!
GoodsID GoodsLibNumber ValueInit
50 8 ★★
51 5 ★
52 200 ★★★★
53 0 ☆
54 20 ★★★

解决方案 »

  1.   

    谢谢楼上的回复,如果将FieldInit表里的规则手工写到SQL语句里是可以得到输出结果的,请问如何将
    FieldInit表直接写到SQL语句里呢?SELECT GoodsID, GoodsLibNumber, 
          'ValueInit' = CASE WHEN GoodsLibNumber = 0 THEN '☆' WHEN GoodsLibNumber >= 1
           AND 5 >= GoodsLibNumber THEN '★' WHEN GoodsLibNumber > 5 AND 
          8 >= GoodsLibNumber THEN '★★' WHEN GoodsLibNumber > 8 AND 
          20 >= GoodsLibNumber THEN '★★★' WHEN GoodsLibNumber > 20 AND 
          999999999 >= GoodsLibNumber THEN '★★★★★' END
    FROM GoodsInfo
      

  2.   

    楼主的
    FieldInit表 
    ValueInit ValueMem 
    ☆ =0 And 0= 
    ★ >=1 And 5>= 
    ★★ >5 And 8>= 
    ★★★ >8 And 20>= 
    ★★★★ >20 And 999999999>= 
    数据有问题。
    应该
    FieldInit表 
    ValueInit ValueMem 
    ☆ =0 And 0= 
    ★ >=1 And 5<= 
    ★★ >5 And 8<= 
    ★★★ >8 And 20<= 
    ★★★★ >20 And 999999999<= 
      

  3.   


    这个结果我认为不可取,既然直接case 出,那楼主还要 FieldInit 表干嘛?
      

  4.   

    应该用拼接语句吧
    把FieldInit表 里的 ValueMem 拼接进来 
      

  5.   


    select GoodsID,(select ValueInit from FieldInit where (
    (GoodsLibNumber =0 And 0= GoodsLibNumber and ValueMem='=0 And 0=') or
    (GoodsLibNumber >=1 And 5>= GoodsLibNumber and ValueMem='>=1 And 5>=') or
    (GoodsLibNumber >5 And 8>= GoodsLibNumber and ValueMem='>5 And 8>=') or
    (GoodsLibNumber >8 And 20>= GoodsLibNumber and ValueMem='>8 And 20>=') or
    (GoodsLibNumber >20 And 999999999>= GoodsLibNumber and ValueMem='>20 And 999999999>=')))
     from GoodsInfo
      

  6.   

    上述SQL其实不是我的本意,其实我还是想用上FieldInit 表的
      

  7.   


    select GoodsID,GoodsLibNumber,(select ValueInit from FieldInit where (
    (GoodsLibNumber =0 And 0= GoodsLibNumber and ValueMem='=0 And 0=') or
    (GoodsLibNumber >=1 And 5>= GoodsLibNumber and ValueMem='>=1 And 5>=') or
    (GoodsLibNumber >5 And 8>= GoodsLibNumber and ValueMem='>5 And 8>=') or
    (GoodsLibNumber >8 And 20>= GoodsLibNumber and ValueMem='>8 And 20>=') or
    (GoodsLibNumber >20 And 999999999>= GoodsLibNumber and ValueMem='>20 And 999999999>='))) as '星级'
     from GoodsInfo
      

  8.   

    --------呵呵,没有问题的,例如
    设n=7
    If n>5 And 8>=n Then Response.write'★★' End If
    成立吧
      

  9.   

    谢谢楼上各位兄弟的回复,再次申明一下“上述手工将表达式写入SQL不是我的本意,其实我还是想用上FieldInit 表的”,不知道有没有说清楚
      

  10.   


    理解,其实说句实话,FieldInit设计有问题。要我 我就会设计成为:
    ValueInit   ValueMem           minnuber   maxnuber数据结构:
    ☆         =0 And 0=             0          1
    ★         >=1 And 5>=           1          6
    ★★       >5 And 8>=            6          9
    ★★★     >8 And 20>=           9          21
    ★★★★   >20 And 999999999>=   21         100000000
      

  11.   

    这样的话,就可以轻易算出结果:create table FieldInit(ValueInit nvarchar(50),ValueMem nvarchar(100),minNuber int,maxNuber int)
    insert into FieldInit
    select '☆','=0 And 0=',0,1 union all
    select '★','>=1 And 5>=',1,6 union all
    select '★★','>5 And 8>=',6,9 union all
    select '★★★','>8 And 20>=',9,21 union all
    select '★★★★','>20 And 999999999>=',21,1000000000Create table GoodsInfo(GoodsID int,GoodsLibNumber int)
    insert into GoodsInfo
    select 50, 8 union all
    select 51, 5 union all
    select 52, 200 union all
    select 53, 0 union all
    select 54, 20 select GoodsID,GoodsLibNumber,
    (select ValueInit from FieldInit where 
    GoodsLibNumber >=minNuber and GoodsLibNumber< maxNuber )
     from GoodsInfo
    /*
    --------------结果------------
    50 8 ★★
    51 5 ★
    52 200 ★★★★
    53 0 ☆
    54 20 ★★★
    */
      

  12.   

    “其实说句实话,FieldInit设计有问题。 ”
    是呀,当时FieldInit表是之前就有,作其他用途的,如果不修改表结构的情况下是否可行?当时在ValueMem里写表达式也是无奈之举
      

  13.   


    结果出来了select GoodsID,GoodsLibNumber,( select top 1 ValueInit from 
    (select ValueInit,Substring(Replace(Replace(Replace(ValueMem,'=',''),'>',''),' and ',','),0,
    Charindex(',',Replace(Replace(Replace(ValueMem,'=',''),'>',''),' and ',','))) as minNuber, 
    Substring(Replace(Replace(Replace(ValueMem,'=',''),'>',''),' and ',','),
    Charindex(',',Replace(Replace(Replace(ValueMem,'=',''),'>',''),' and ',','))+1,
    Len(Replace(Replace(Replace(ValueMem,'=',''),'>',''),' and ',',')))+1 as maxNuber 
    from FieldInit) as a
    where GoodsLibNumber>=minNuber and GoodsLibNumber<maxNuber)
    from GoodsInfo
      

  14.   

    ---------
    如果将FieldInit表里的数据修改下,再写一回
    ValueInit ValueMem 
    ☆ 0,0
    ★ 1,5
    ★★ 5,8
    ★★★ 8,20
    ★★★★ 20,999999999
      

  15.   

    select GoodsID,GoodsLibNumber,ValueInit from 
    (select ValueInit,
    minv =case when charindex('=',left(valuemem,2)) >0 then convert(int,substring(replace(replace(valuemem,'=',''),'>',''),0,charindex('and',replace(replace(valuemem,'=',''),'>',''))))-1 
     else convert(int,substring(replace(replace(valuemem,'=',''),'>',''),0,charindex('and',replace(replace(valuemem,'=',''),'>','')))) end, 
    maxv = case when charindex('=',right(valuemem,2)) >0 then
    convert(int,substring(replace(replace(valuemem,'=',''),'>',''),charindex('and',replace(replace(valuemem,'=',''),'>',''))+3,len(replace(replace(valuemem,'=',''),'>',''))))+1
     else convert(int,substring(replace(replace(valuemem,'=',''),'>',''),charindex('and',replace(replace(valuemem,'=',''),'>',''))+3,len(replace(replace(valuemem,'=',''),'>','')))) end  from tb1) t  
    inner join tb2 on tb2.goodslibnumber > minv and tb2.goodslibnumber < maxv 
      

  16.   


    MSN联系我吧,和你一起商讨下。
    MSN:[email protected]
      

  17.   

    select GoodsID,GoodsLibNumber,(select top 1 ValueInit from (select ValueInit,cast(right(ValueLeft,len(ValueLeft)-(case when left(ValueLeft,2)='>=' then 1 else 0 end))as int)+(case when charindex('=',ValueLeft)=0 then 1 else 0 end)as ValueStart,cast(left(ValueRight,len(ValueRight)-(case when right(ValueRight,2)='>=' then 2 else 1 end))as int)-(case when charindex('=',ValueRight)=0 then 1 else 0 end)as ValueEnd from(select ValueInit,left(ValueMem,charindex(' And ',ValueMem))as ValueLeft,right(ValueMem,len(ValueMem)-charindex(' And ',ValueMem)-4)as ValueRight from FieldInit)as T)as T1 where ValueStart>=GoodsInfo.GoodsLibNumber and ValueEnd<=GoodsInfo.GoodsLibNumber)as ValueInit from GoodsInfo
      

  18.   


    -----数据----
    create table FieldInit(ValueInit nvarchar(50),ValueMem nvarchar(100))
    insert into FieldInit
    select '☆','=0 And 0='union all
    select '★','>=1 And 5>='union all
    select '★★','>5 And 8>='union all
    select '★★★','>8 And 20>=' union all
    select '★★★★','>20 And 999999999>='Create table GoodsInfo(GoodsID int,GoodsLibNumber int)
    insert into GoodsInfo
    select 50, 8 union all
    select 51, 5 union all
    select 52, 200 union all
    select 53, 0 union all
    select 54, 20 ----定义2个自定义函数:---
    ---分别为:-----
    ---返回 minNuber-----
    Create function f_test(@a nvarchar(1000))
    returns nvarchar(1000)
    as
    begin
    declare @Sum int 
    set @Sum=0
    if Substring(@a,0,3) <> '>=' and Substring(@a,0,2) <> '='
    begin
    set @Sum=1
    end
    set @a=Substring(Replace(Replace(Replace(@a,'=',''),'>',''),' and ',','),0,Charindex(',',Replace(Replace(Replace(@a,'=',''),'>',''),' and ',',')))
    set @a=cast((cast(@a as int)+@Sum)as nvarchar(1000))
    return @a
    end
    ---返回 maxNuber-----
    Create function f_test2(@a nvarchar(1000))
    returns nvarchar(1000)
    as
    begin
    set @a=Substring(Replace(Replace(Replace(@a,'=',''),'>',''),' and ',','),
    Charindex(',',Replace(Replace(Replace(@a,'=',''),'>',''),' and ',','))+1,
    Len(Replace(Replace(Replace(@a,'=',''),'>',''),' and ',',')))+1
    return @a
    end----SQL---------
    select GoodsID,GoodsLibNumber,( select ValueInit from FieldInit
    where GoodsLibNumber>=dbo.f_test(ValueMem) and GoodsLibNumber<dbo.f_test2(ValueMem))
    from GoodsInfo-----结果--------
    50      8       ★★
    51      5       ★
    52      200     ★★★★
    53      0       ☆
    54      20      ★★★
      

  19.   

    结贴了,谢谢izbox的耐心交流,问题已经解决了.