客户表(A):
    aid      aname
     1        abc
     2        efg
     3        hij
 
定单表(B):
    bid       aid
     1         1
     2         1
     3         2
     4         2
     5         3
产品表(C):
    cid   cdj(产品单价)  cnum(定购数量)    bid
     1     1000            10               1
     2     500              2               2
     3     2800            15               3
     4     3000             2               4
     5     520             10               2
     6     720              9               1问题如下:
    查出每个客户的编号、客户名以及总定单金额??
请哪位大虾帮忙解决一下,小弟在这先谢了!!!
     
    

解决方案 »

  1.   


    create table a
    (
      aid int primary key identity(1,1),
      name varchar(20)
    )
    insert into a
    select 'abc' union all
    select 'efg' union all
    select 'hij'
    create table b
    (
      bid int primary key identity(1,1),
      aid int
    )
    insert into b
    select 1 union all
    select 1 union all
    select 2 union all
    select 2 union all
    select 3
    create table c
    (
      cid int primary key identity(1,1),
      price int,
      quantity int, 
      bid int
    )
    insert into c
    select 1000,10,1 union all
    select 500,2,2 union all
    select 2800,15,3 union all
    select 3000,2,4 union all
    select 520,10,2 union all
    select 720,9,1select a.aid,min(a.name) as name ,isnull(sum(c.price*c.quantity),0) as total
    from a left join b on b.aid = a.aid
           left join c on c.bid = b.bid
    group by a.aid
      

  2.   

    select a.aid,min(a.name) as name ,isnull(sum(c.price*c.quantity),0) as total from c where cid in (select bid from a left join b on b.aid = a.aid)
      

  3.   

    使用Hibernate 把导航关系设置好  一条语句OK!!
      

  4.   

    select a.aid as id ,a.name as name ,isnull(sum(c.price*c.quantity),0) as total from c where cid in (select bid from a left join b on b.aid = a.aid)
      

  5.   

    你看看关于:sum(case when ..then ..else。。 end)的用法,看完之后你就一定能写出来了。
      

  6.   

    多谢各位大虾了!
      只是我想问的是用Hibernate怎么写HQL语句呢??
    关系我也已经映射好了!!
    待求高人解答????