分组时从子查询中计算行数

时间:2014-07-16 03:38:22

标签: mysql count group-by subquery

我需要帮助计算MySQL中子查询中行的结果,我有这样的事情:

SELECT t.Name, 
       t.Person,
       (SELECT COUNT(pp.id) FROM pp JOIN account s ON s.id = pp.account WHERE s.person = t.id HAVING SUM(pp.total) >= (i.total - 250))'Total Accounts', 
FROM (SELECT p.id, n.name 'Name', p.name 'Person' 
      FROM names n 
      JOIN person p ON p.id = n.person) t
      JOIN payInfo i
GROUP BY t.Name

我希望在子查询

中使用GROUP BY得到类似的东西
SELECT t.Name, 
       t.Person,
       (SELECT COUNT(pp.id) FROM pp JOIN account s ON s.id = pp.account WHERE s.person = t.id GROUP BY s.id HAVING SUM(pp.total) >= (i.total - 250))'Total Accounts', 
FROM (SELECT p.id, n.name 'Name', p.name 'Person' 
      FROM names n 
      JOIN person p ON p.id = n.person) t
      JOIN payInfo i
GROUP BY t.Name

我收到此错误:“错误代码:1242。子查询返回超过1行”

0 个答案:

没有答案