如何使用2表获取此查询

时间:2015-12-26 17:33:37

标签: 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
我的问题是:
==================
1.如何在每个test_id上找到最大值 2.如何获得或完全填写申请人_id的地方 比如这样:
test_id | result_max | total_applicant_status(complete)
1 | 90 | 2
2 | 80 | 2
3 | 90 | 2
4 | 80 | 1

1 个答案:

答案 0 :(得分:1)

SELECT MAX(value) FROM table WHERE test_id = 1;

或者SELECT value, test_id FROM table ORDER BY value DESC;

对于下一部分,这可能会给你想要的东西。

SELECT at.test_id, MAX(at.result), COUNT(IF(status='complete', 1, 0)) FROM application a LEFT JOIN application_test at ON a.id = at.application_id GROUP BY application_id;