gpt4 book ai didi

php - 如何在mysql中计算group by后的行数或总行数

转载 作者:行者123 更新时间:2023-11-29 21:40:27 25 4
gpt4 key购买 nike

为什么我有两张 table

1.应用

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

最佳答案

使用子查询,

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)

关于php - 如何在mysql中计算group by后的行数或总行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34574461/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com