我有一些表格可以根据最终得分排序的查询来制作表格。 我可以使用几个查询和 php 来实现它,但我不知道如何按 Final_score DESC 进行排序。 我使用 mysql 5.6 和 PHP 请帮忙。
现有表
表佩塞塔
| id_peserta | name |
| 1 | mr. one |
| 2 | mr. two |
表汝来
| id_nilai | id_peserta | nilai_a | nilai_b | nilai_c|
| 1 | 1 | 70 | 80 | 90 |
| 2 | 2 | 85 | 95 | 75 |
表格中心
| id_center | center_name |
| 1 | center1 |
| 2 | center2 |
| 3 | center3 |
表格文章
| id_essay | id_center | id_peserta | nilai_e |
| 1 | 1 | 1 | 65 |
| 2 | 2 | 1 | 60 |
| 3 | 3 | 1 | 50 |
| 4 | 1 | 2 | 55 |
我需要这样的东西:
| id_peserta | name | center1 | center2 | center3 | total_center | nilai_a | nilai_b | nilai_c | total_nilai | final_score | passed/failed |
| 1 | mr. one | 65 | 60 | 50 | 175=65+60+50 | 70 | 80 | 90 | 250=70+80+90 | 425=175+250 | passed |
| 2 | mr. two | 55 | | | 55 | 85 | 75 | 95 | 265=85+75+95 | 320=55+265 | failed |
按最终得分排序
最佳答案
作为起点:
select n.id_peserta, n_sum+coalesce(e_sum,0) as final_score, center_count from
(select id_peserta, sum(nilai_a+nilai_b+nilai_c) as n_sum from nilai
group by id_peserta, nilai_a+nilai_b+nilai_c) n
left join
(select id_peserta, sum(nilai_e) as e_sum, count(1) as center_count from essay
group by id_peserta) e
on n.id_peserta = e.id_peserta
order by n_sum+coalesce(e_sum,0) DESC
“nilai”的数量似乎是静态的(已经是表结构的一部分)。但中心的数量被设计为动态的。因此,我不会“交叉制表”它,而是显示访问过的中心的计数。
挑战数据模型设计可能是值得的,但上面应该根据给定的结构为您提供所需的答案。
工作假设:nilai.id_peserta 是一个唯一的 key 。
问候 拉尔夫
(用文本编辑器编写,未经测试,因此可能会遇到拼写错误)
关于php - 如何查询多个表并使用附加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40937694/