简体   繁体   English

phpmyadmin中一列的总和为另一列的总和

[英]Sum of one column as another column in phpmyadmin

Using phpmyadmin I am trying to make the sum of one column "PointsAwarded" in one table "tasks" be the values in another column "points" in another table "users". 使用phpmyadmin我试图使一个表“任务”中的一列“ PointsAwarded”的总和成为另一表“用户”中另一列“ points”中的值。 Each user will get points for a task, i want to be able to have the total points they have received be a column in the "user" table. 每个用户将获得一项任务的积分,我希望能够将他们获得的总积分作为“用户”表中的一列。 Or an alternative way to show this data in my php datagrid. 或以其他方式在我的php datagrid中显示此数据。

new C_DataGrid("SELECT id, FirstName, LastName, Points FROM users", "id", 
"Ranking");

that is the format of the datagrid I am using, it is a phpGrid format. 那是我正在使用的datagrid的格式,它是phpGrid格式。 Any help is much appreciated and any further info you need just ask. 非常感谢您的帮助,您需要询问更多信息。

Here is the ranking.php page 这是rank.php页面

<?php
// use phpGrid\C_DataGrid;

include_once("../phpGrid/conf.php");
include_once('../inc/head.php');
?>

<h1><a href="../index.php">Staff Rank - Project Management System</a></h1>

<?php
$_GET['currentPage'] = 'ranking';
include_once('../inc/menu.php');
?>

<h3>Ranking</h3>

<?php
$dgRank = new C_DataGrid("SELECT id, FirstName, LastName, Points FROM 
users", "id", "Ranking");
$dgRank->set_col_hidden('id');
$dgRank-> set_sortname('id');
$dgRank->enable_edit();
$dgRank -> display();
?>

<?php
include_once('../inc/footer.php');
?>

Image Album of Database Structure/Relationship https://imgur.com/a/DGPkDPa 数据库结构/关系图片集https://imgur.com/a/DGPkDPa

Album of databases and application: https://imgur.com/a/NKrDLLx 数据库和应用程序相册: https : //imgur.com/a/NKrDLLx

This is a purely database design issue. 这纯粹是数据库设计问题。

Considering that the Points are computed from another table, you should not have this column on your user table. 考虑到这些Points是从另一个表计算出来的,因此您的用户表上不应包含此列。 You can DROP it. 您可以删除它。

The points should be computed in SQL. 点数应使用SQL计算。 There are multiple ways to do this and here's an example. 有多种方法可以做到这一点,这是一个示例。

Create a VIEW that computes the points for all users, based on the PointsAwarded for their tasks. 创建一个VIEW,根据其任务的PointsAwarded计算所有用户的积分。 Launch this query in phpmyadmin : 在phpmyadmin中启动此查询:

CREATE VIEW User_Points AS
SELECT  u.id, SUM(COALESCE(t.PointsAwarded,0)) AS Points 
FROM users 
LEFT JOIN tasks t ON u.id=t.EmployeeID
GROUP BY u.id

Now you can do a SELECT * FROM User_points and that will return something like this: 现在,您可以执行SELECT * FROM User_points ,这将返回以下内容:

id  Points
1   48
2   0
3   256
4   125

with id being the id of the user. id是用户的ID。

Now in the PHP, change your query to this : 现在在PHP中,将查询更改为:

SELECT u.id, u.FirstName, u.LastName, p.Points FROM users u INNER JOIN User_points p ON u.id=p.id

and all your issues will be solved 您所有的问题都将得到解决

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM