现在有两个表,表一是物品的属性表,表二是物品表属性表                  物品表属性a                  物品1
属性b                  物品2
属性c                  物品3
属性d                  物品4
属性e                  物品5物品拥有的属性不是固定的(例如:物品1拥有属性a,c   物品2拥有属性c,d)请问如何在物品表中查询出同时拥有属性c和属性e的物品要实现相应的查询功能,数据库有好的设计方法吗?

解决方案 »

  1.   

    物品属性表物品id   属性id
    ---------------物品1id  属性aid
    物品1id  属性bid
    物品2id  属性aid
    物品2id  属性cidselect 物品,属性 from  物品属性表 a,属性表 b, 物品表 c
    where a.物品id=c.物品id 
    and a.属性id=b.属性id
    and 属性=属性c 
    and 属性= 属性e
      

  2.   

    select * from 物品表
    where 物品表.属性=c and 物品表.属性=e
    你最好说详细点,我是按自己的理解来的.
      

  3.   

    select * from goods; G_ID G_NAME
    ----- ------------------------------
        1 物品1
        2 物品2
        3 物品3
        4 物品4
        5 物品5SQL> select * from attr;T_ID       T_NAME
    ---------- ---------------
    A          属性a
    B          属性b
    C          属性c
    D          属性d
    E          属性eselect * from relation; G_ID T_ID
    ----- --------------------
        1 E
        1 C
        2 E
        2 C
        3 E
        3 C
        3 A
        4 E
        5 CSQL> select g_name from goods t1 where not exists (
      2  select * from attr t2 where t_id in ('E', 'C') and not exists (
      3  select * from relation t3 where t1.g_id=t3.g_id and t2.t_id=t3.t_id));G_NAME
    --------------------------------------------------
    物品1
    物品2
    物品3不知道是不是这个意思
      

  4.   

    或者 做 5个表连接SQL> select distinct t1.g_name from goods t1, attr t2, relation t3, attr t4, relation t5
      2  where t1.g_id=t3.g_id and t2.t_id=t3.t_id and t2.t_id='E' AND
      3  t1.g_id=t5.g_id and t4.t_id=t5.t_id and t4.t_id ='C';G_NAME
    --------------------------------------------------
    物品1
    物品2
    物品3
      

  5.   

    目的:在物品表里查出含有属性c和属性e的物品编号
    在属性表里有“属性ID,属性名“ 两个子段在物品表里,每个物品可能含有a属性,也可能含有a,c两种属性或者更多属性,也就是说每个物品拥有任意个属性如果只建这两个表,能实现这个查询吗?如果能实现,物品表里的属性字段应该怎么设计?如果两个表不能实现,像1楼那样再建第三个表,查询语句的效率上有什么要注意的吗?