服装可能有多种颜色方法一:1代表红色
2代表黄色
3代表绿色
4代表蓝色
……  ID     名称        颜色
  201    上衣      1,2,3         
  202    上衣      1,2,3,4
  203    上衣      2,3,4方法二:1代表红色
2代表黄色
4代表绿色
8代表蓝色
以后都为2的倍数
……  ID         颜色
  201        7
  202        15
  203        14颜色那列保存各数字之和
实际中颜色的种类和每件衣服拥有的颜色属性都会多很多能不能请教一下哪种方法好,尤其是第2种方法比第一种方法好在哪里?第二种方法在解析颜色属性列的时候,有什么优点?
如果可以的话,能不能写一点代码出来让我学习下如何实现这种算法的啊。
还有,再设计表的时候,是不是要尽量避免出面第一种方法里面那种需要拆分字段串的情况出现啊
问了不少问题,麻烦大家了。

解决方案 »

  1.   

    其实就是解析 用二进制解析 经常使用if(object_id('Color') > 0)
       drop table Color
    go
    create table Color(
       colorid int, 
       decription nvarchar(10)
    )
    insert into Color
    select 1,'红色' union all select 2,'黄色' 
    union all select 4,'绿色' union all select 8,'蓝色'if(object_id('ColorDetail') > 0)
      drop table ColorDetail
    go
    create table ColorDetail(
       id nvarchar(10),
       Color int 
    )
    insert into ColorDetail
    select '201',7 union all select '202',15 union all select '203',14--比如我要查有红色衣服颜色的所有衣服的话
    select * from ColorDetail where Color|1 = Color 
    --比如我要有红色衣服和黄色衣服颜色的所有衣服的话
    select * from ColorDetail where Color|1 = Color and Color|2 = Color