1
SELECT tblorder.fld_userid
FROM `tblordereditems`
inner JOIN tblorder ON tblorder.fld_id = tblordereditems.fld_order_id
where (SELECT sum( tblordereditems.fld_product_quantity )FROM tblordereditems inner JOIN tblorder ON tblorder.fld_id =tblordereditems.fld_order_id GROUP BY tblorder.fld_userid)>5  

这是我的查询,但会引发错误。任何机构都可以帮忙吗?谢谢

这里有三张桌子。

  1. tbluser
  2. tblorder
  3. tblordereditems

tbluser 的字段 fld_id 是 tblorder 作为 fld_userid 的外键。
tblorder 的 fld_id 是 tblordereditems 作为 fld_order_id 的外键,我得到结果 fld_userid 订购了 5 件以上的产品,即。fld_product_quantity 按 tblorder.fld_userid 分组的总和

4

1 回答 1

1

它返回超过 1 行,因为您正在请求所有用户的计数。

尝试:

Select tb1.fld_userid
FROM `tblordereditems` tbi1
inner JOIN tblorder tb1 ON tb1.fld_id = tbi1.fld_order_id
where (
        SELECT sum( tbi2.fld_product_quantity ) 
        FROM tblordereditems tbi2 inner JOIN tblorder tb2 ON tb2.fld_id =tbi2 .fld_order_id 
         where tb2.fld_userid = tb1.fld_userid
)>5
于 2012-06-26T08:48:20.173 回答