有一基本特性表 TBase
int  varchar(20) //字段 BRecNO  BName
1    'a'         //RecNO 是自增量  
2    'as'
3    'sd' ......
有一物料表 TMat
int varchar(30) //字段 MRecno MName
1   'PVC'
2   'PVX' ....  
物料表中一个物料可有多种特性,就有不同价格 例:
'PVC' 1  有特性 1,2  价格 1.0
      2  有特性 3,4  价格 1.2
      3  有特性 3,6,8 价格 1.4
现要求按特性查询 例:
物料 'PVC' 特性 3 可查到 2和3
          特性 3,8 可查到 3
          特性 3,4,8 没有
提问: 1.怎样设计物料价格对应表?
      2.查询语句怎样写?(速度快)

解决方案 »

  1.   

    1 特性表中每一个特性有独立的单价
    通过物料表可以这样查
    select * from 物料表 where 产品 = 'PVC' and charindex('3',特性) > 0
      

  2.   

    自己找到解决方法了.
    建物料价格表--与特性相关(TMatPrice)
    结构是 序号(Recno int)物料(matid int)价格(price float)特性(proc varchar(50))
    例 Recno matid  price  proc
       1     1      1.0    ',1,2,'
       2     1      1.2    ',3,4,'
       3     1      1.4    ',3,6,8,'
    select * from Tmatprice where charindex(',3,',proc)>0
    结果是 recno 2和3
    select * from tmatprice where charindex(',3,',proc)>0 and charindex(',8,',proc)>0
    结果是 recno 3
    select * from tmatprice where charindex(',3,',proc)>0 and charindex(',8,',proc)>0 and charindex(',4,',proc)>0
    结果是 空
      

  3.   

    我的设计create table TBase(BRecNO int identity(1,1), BName varchar(20))
    insert into TBase
    select 'a'
    union all select 'as'
    union all select 'sd'
    go
    create table TMat(MRecno int identity(1,1),MName varchar(30))
    insert into TMat
    select 'PVC'
    union all select 'PVX'
    create table TBaseMat(BMno int identity(1,1), strBRecNO nvarchar(20),price decimal(5,2))
    insert into TBaseMat
    select '1,2',1.0
    union all select  '3,4', 1.2
    union all select   '3,6,8', 1.4 
    在查询时需要建个函数才能完成,否则不好查create function getBRecNO(@p1 nvarchar(20),@p2 nvarchar(20))
    returns int
    as 
    begin
    set @p1=','+@p1+','
    set @p2=','+@p2+','
    declare @i int,@j int
    set @j = 1
    set @i = 1
    declare @str nvarchar(20)
    set @str = substring(@p2,@i,charindex(',',@p2,@i+1)-@i+1)
    while(@str<>'')
     begin
        if(charindex(@str,@p1)=0) 
          begin
             set @str=''
          end
         else
          begin
            set @j=@j+1
            set @i = charindex(',',@p2,@i+1)
            if(charindex(',',@p2,@i+1)=0)
              set @str = ''
            else
              set @str = substring(@p2,@i,charindex(',',@p2,@i+1)-@i+1)
          end
       end
    if(@j=len(@p2)-len(replace(@p2,',','')))
    begin  
     set @j=1
    end
    else
    begin
      set @j = 0
    end
    return @j
    end可以满足的需要的所有查询:declare @pre nvarchar(20)
    set @pre = '3,8'
    select BMno from TBaseMat where dbo.getBRecNO(strBRecNO,@pre)=1
    /*
    BMno
    -----------
    3
    */
       
      

  4.   

    select * from TMat where Charindex('3,', property) > 0 
    or Charindex(',3', property) > 0不一定会有2个引号的