三张表:
prod  产品表
prodform  订单表
auxitem  辅助明细表三张表关系:
prod ,prodform 通过prodid(产品id)联系
prod,auxitem  通过prodcolorid(产品颜色id)对应于auxitemid。
prodform,auxitem 通过 prodformtype(订单类型)对应于auxitemid。
注:表示颜色的辅助明细和表示订单的辅助明细是两条记录,如下:
auxitemid    auxitemname
12             红色
23             常规订单要求:
写一条sql语句要求返回订单信息,订单中包含的商品信息。
select * from prodform,prod,auxitem where prodformtype=auxitemid and prodcolorid=auxitemid and prodform.prodid = prod.prodid.
上面这条语句是查不出来结果的。
到底该怎么写,希望各位好人帮帮忙

解决方案 »

  1.   

    去掉一个and条件试试
    select * from prodform,prod,auxitem where prodformtype=auxitemid and  prodform.prodid = prod.prodid
      

  2.   

    其实有用的就是下面这些:
    prod:          prodid,prodname,prodcolorid       eg:1,IBM thinkpad,1
    prodform:      prodformid,prodid,prodformtype    eg:1,    1,         2
    auxitem:       auxitemid,auxitemname             eg:1,  黑色 
                                                        2, 常规订单现在要查的是:  
    prodformid, prodformtype,   prodname,      prodcolor
    1              常规订单     IBM thinkpad        黑色
    用一条语句写出来
      

  3.   

    if object_id('pubs..prod') is not null
       drop table prod
    go
    create table prod
    (
    prodid      int,
    prodname    varchar(20),
    prodcolorid int
    )
    insert into prod(prodid,prodname,prodcolorid) values(1,'IBM thinkpad',1)if object_id('pubs..prodform') is not null
       drop table prodform
    gocreate table prodform
    (
    prodformid   int,
    prodid       int,
    prodformtype int
    )
    insert into prodform(prodformid,prodid,prodformtype) values(1,1,2)if object_id('pubs..auxitem') is not null
       drop table auxitem
    gocreate table auxitem
    (
    auxitemid   int,
    auxitemname varchar(10)
    )
    insert into auxitem(auxitemid,auxitemname) values(1,'黑色')
    insert into auxitem(auxitemid,auxitemname) values(2,'常规订单')select n.prodformid , n.prodformtype , m.prodname , m.prodcolor from
    (
    select a.prodid , a.prodname , b.auxitemname as prodcolor
    from prod a , auxitem b
    where a.prodcolorid = b.auxitemid
    ) m,
    (
    select c.prodformid , d.auxitemname as prodformtype
    from prodform c , auxitem d
    where c.prodformtype = d.auxitemid
    ) n
    where m.prodid = n.prodformid
    drop table prod
    drop table prodform
    drop table auxitem
    prodformid  prodformtype prodname             prodcolor  
    ----------- ------------ -------------------- ---------- 
    1           常规订单         IBM thinkpad         黑色(所影响的行数为 1 行)