如何在mysql中的groupby之后的行数或总行数

时间:2016-01-03 07:53:57

标签: php mysql

Hy我有2张桌子

一,应用

id | name    | status
====================
1  | morvick | complete
2  | siti    | prosess
3  | boby    | complete

2.application_test

id | application_id | test_id | result
======================================
1 | 1 | 1 | 70
2 | 1 | 2 | 80
3 | 1 | 3 | 90
4 | 2 | 1 | 60
5 | 2 | 2 | 80
6 | 2 | 3 | 70
7 | 3 | 1 | 90
8 | 3 | 2 | 70
9 | 3 | 3 | 60
10| 3 | 4 | 80

Myquery喜欢这个:

SELECT test_id, 
SUM(IF(app.status='complete',apt.result,0)) AS complete_sum, 
SUM(IF(app.status='process',apt.result,0)) AS process_sum  
FROM application_test AS apt 
JOIN application AS app ON app.id=apt.application_id 
GROUP BY apt.test_id

并且是这样的:
test_id | SUM(result = complete) | SUM(result = proses) |
1 | 90 | 50
2 | 80 | 40
3 | 90 | 60
4 | 80 | 70

我的问题是:在查询GROUPBY后,如何获取总行数或计数行数?例如, 4总数

2 个答案:

答案 0 :(得分:0)

这就是你想要的吗?

SELECT (select count(*) from application_test) as total_row, 
test_id, 
SUM(IF(app.status='complete',apt.result,0)) AS complete_sum, 
SUM(IF(app.status='process',apt.result,0)) AS process_sum  
FROM application_test AS apt 
JOIN application AS app ON app.id=apt.application_id 
GROUP BY apt.test_id

答案 1 :(得分:0)

使用子查询,

Select *, Count(*) as total_rows From(
SELECT test_id, 
SUM(IF(app.status='complete',apt.result,0)) AS complete_sum, 
SUM(IF(app.status='process',apt.result,0)) AS process_sum  
FROM application_test AS apt 
JOIN application AS app ON app.id=apt.application_id 
GROUP BY apt.test_id)