三表查询数据不对,请教:t_order(订单表)
userId   用户ID
order_id   订单ID
order_bets 订单金额t_order_Product(订单产品关联表)
order_id    订单ID
Product_id  产品ID
product_shares 产品份数t_product(产品表)
product_id   产品ID
product_name 产品名称select nvl(sum(a.order_bets),0) from t_order a, t_order_Product b, t_product c
where a.order_id = b.order_id and b.order_id = c.order_id
and a.userId = xxx and c.product_name = 'name'统计出来的数据不正确,请大家帮忙,急

解决方案 »

  1.   

    select sum(nvl(a.order_bets,0)) from t_order a, t_order_Product b, t_product c 
    where a.order_id = b.order_id and b.order_id = c.order_id 
    and a.userId = xxx and c.product_name = 'name' 
      

  2.   

    你不会是统计的值为null吧,那样的话把nvl放到sum里面去.
      

  3.   

    我认为lz的sql  错了  ,你能说一下你到底要查询什么  ,同时说明表结构  中的主键 和外键
      

  4.   

    t_order(订单表) 
    userId   用户ID 
    order_id  订单ID  PK
    order_bets 订单金额 t_order_Product(订单产品关联表) 
    order_prinduct_id PK
    order_id    订单ID 
    Product_id  产品ID 
    product_shares 产品份数 t_product(产品表) 
    product_id  产品ID PK
    product_name 产品名称 select nvl(sum(a.order_bets),0) from t_order a, t_order_Product b, t_product c 
    where a.order_id = b.order_id and b.order_id = c.order_id 
    and a.userId = xxx and c.product_name = 'name' 数据库只建了PK约束,所有建立FK约束
      

  5.   

    select nvl(sum(a.order_bets), 0)
      from t_order a, t_order_Product b, t_product c
     where a.order_id = b.order_id
       and b.Product_id = c.product_id   -- 你这个关联应该改为这样。
       and a.userId = xxx
       and c.product_name = 'name'
      

  6.   

    select nvl(sum(a.order_bets),0) from t_order a, t_order_Product b, t_product c 
    where a.order_id = b.order_id and b.Product_id = c.product_id 
    and a.userId = xxx and c.product_name = 'name'
      

  7.   

    支持,呵!t_product(产品表) 中都没有order_id这个字段,怎么用order_id和t_order_Product关联,LZ居然犯这种小错啊,细心细心!!!
      

  8.   

    订单关联表(t_order_Product)中有order_id,用这个字段关联不行呀???????????????????????SQL语句这个地方是模拟错了,应该是这样的select sum(nvl(a.order_bets,0)) from t_order a, t_order_Product b, t_product c 
    where a.order_id = b.order_id and b.product_id  = c.product_id 
    and a.userId = xxx and c.product_name = 'name' 但这样统计的结果还是不对!!!!!
      

  9.   

    select nvl(sum(a.order_bets),0) from t_order a, t_order_Product b, t_product c 
    where a.order_id = b.order_id and b.order_id = c.order_id 
    and a.userId = xxx and c.product_name = 'name' 解释你这个SQL:
    其实你要统计的是某个单品的花费。
    在你的3个表中:
    t_order(订单表) 
    userId   用户ID 
    order_id  订单ID  PK 
    order_bets 订单金额 t_order_Product(订单产品关联表) 
    order_prinduct_id PK 
    order_id    订单ID 
    Product_id  产品ID 
    product_shares 产品份数 t_product(产品表) 
    product_id  产品ID PK 
    product_name 产品名称 
    只有t_order设计到了金额,但是这个金额是该定单的金额。
    一张定单可能有很多个单品,所以你的理解SQL存在逻辑问题。
    目前你提供的3个表无法进行单品统计。
      

  10.   

    当然除非你的数据提供中一张定单只有一个品项,那么你的SQL就是对的。
      

  11.   

    我的订单却实只有一种商品,我的业务需要是这样的:某一份产品假设它是10元,我把它分成10份,每份一块,我将根据订单的金额来关联产品的份数,所以设计了一个中间表,用中间表来关联订单表和产品表,中间表同时记录该产品的份数,如果该产品份数全部认购完成,系统将在产品表中再产生一个同样的产品,供未来的订单关联使用,这样一来,我的系统中最多只存在一个没有被关联的产品或是一个没有被全部关联完成的产品,这个没有被关联的产品我是需要自已付钱的!!!select sum(nvl(a.order_bets,0)) from t_order a, t_order_Product b, t_product c 
    where a.order_id = b.order_id and b.product_id  = c.product_id 
    and a.userId = xxx and c.product_name = 'name' 我的这个查询产生一个笛卡尔乘积,没有得到正确的结果,我在想,是不是我的表关系不对所致,我的订单与关联表的关系是一对多的关联,例如一个8块的订单过来,应该关联产品8份,而系统中如果没有足够的8份,我得再产生一次订单,这样一来,我的中间表中就存在二条记录对应于产品表,我的产品与关联表也是一对多的关系,一个产品可能被多个订单所关联,还得请各位大大帮忙,感觉自已SQL学的不扎实!!!!
      

  12.   

    我的订单却实只有一种商品,我的业务需要是这样的:某一份产品假设它是10元,我把它分成10份,每份一块,我将根据订单的金额来关联产品的份数,所以设计了一个中间表,用中间表来关联订单表和产品表,中间表同时记录该产品的份数,如果该产品份数全部认购完成,系统将在产品表中再产生一个同样的产品,供未来的订单关联使用,这样一来,我的系统中最多只存在一个没有被关联的产品或是一个没有被全部关联完成的产品,这个没有被关联的产品我是需要自已付钱的!!! select sum(nvl(a.order_bets,0)) from t_order a, t_order_Product b, t_product c 
    where a.order_id = b.order_id and b.product_id  = c.product_id 
    and a.userId = xxx and c.product_name = 'name' 我的这个查询产生一个笛卡尔乘积,没有得到正确的结果,我在想,是不是我的表关系不对所致,我的订单与关联表的关系是一对多的关联,例如一个8块的订单过来,应该关联产品8份,而系统中如果没有足够的8份,我得再产生一次产品,这样一来,我的中间表中就存在二条记录对应于产品表,我的产品与关联表也是一对多的关系,一个产品可能被多个订单所关联。修正一下
      

  13.   


    这样也是不对的,因为虽然一张定单只有一个产品项,但是同一个产品他有可能分两次或多次存放进去的,所以会造成多条笛卡尔积,就算是只有一个产品ID,也要先distinct一下,再关联才有效。当然如果一张定单里有多个产品ID的话,就无论如何也不会有正确的结果,建议修改表结构。