简体   繁体   English

从一个表中选择数据,按另一表中的数据总和排列

[英]Select data from one table, arrange by a sum of data from another

A client is looking for a points system to be implemented on her website, I'm struggling to display the users based upon the amount of points collected, I hope somebody may be able to help me out here and point me in the right direction to getting this code to work properly. 客户正在寻找要在其网站上实施的积分系统,我正在努力根据收集的积分数量来向用户显示,我希望有人可以在这里为我提供帮助,并向我指出正确的方向使此代码正常工作。

I am selecting all data from ap_users and in the code I am also trying to select all data from ap_points although I do not require all the data from either tables, to be specific I only require: 我正在从ap_users选择所有数据,并且在代码中,我也试图从ap_points选择所有数据,尽管我不需要任何一个表中的所有数据,具体来说,我只需要:

ap_users:
     user_id
     first_name
     last_name
     display_img
     hub_access

ap_points:
     user_id
     points_added

I thought that selecting ALL data may be the easiest route, will let you decide. 我认为选择所有数据可能是最简单的方法,让您决定。

I am trying to select and display all users where hub_access = '1' and order by the total points_added by highest first. 我正在尝试选择并显示hub_access = '1'所有用户, hub_access = '1'points_added最高的顺序排序。 Points are added separately by rows and need to be added up (which is why I have the sum function). 点是按行分别添加的,因此需要加起来(这就是为什么我有sum函数的原因)。

$sql = "SELECT * FROM `ap_users`, `ap_points` WHERE `hub_access` = '1' ORDER BY sum(points_added) DESC";

I also tried configuring it to be specific tables like: 我还尝试将其配置为特定的表,例如:

ap_users.hub_access and ORDER BY sum(ap_points.points_added) but these did not work either. ap_users.hub_accessORDER BY sum(ap_points.points_added)但它们也不起作用。

This current code is either showing no results or a single result with no errors displaying? 当前代码是没有显示结果还是显示没有错误的单个结果? I'm not sure whether I may need to use some kind of Group By function to connect the user_ids from both tables ? 我不确定是否可能需要使用某种Group By函数来连接两个表中的user_id?

SUM is an aggregating function. SUM是一种汇总功能。 You should be grouping by user_id if you want the sum for each user_id. 如果要每个user_id的总和,则应按user_id分组。

Something like 就像是

SELECT *, sum(points_added) as sum_points FROM app_users 
JOIN app_points ON app_users.user_id = app_points.user_id
WHERE `hub_access` = '1' 
GROUP BY app_users.user_id
ORDER BY sum_points;

I have not tested that query, but that should give you an idea of the solution. 我没有测试过该查询,但是应该可以使您对解决方案有所了解。

暂无
暂无

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

相关问题 MySQL我可以从一个表还是另一个表中选择数据? - MySQL Can I Select Data from One table OR another Table? 从一个表中选择并包括来自另一个表的总和 - Select From One Table And Include Sum Value From Another 如何从一个表中选择两个SUM,但只能从一个表中选择一个SUM。 数据 - How to select two SUMs from one table but one SUM must be made from only max. data Select 表中的数据使用另一个表中的数据 - Select data from table using data from another table PHP mysqli。 在一个表之后执行ORDER SELECT但从另一个表中选择数据? - PHP mysqli. ORDER SELECT after one table but SELECT data from another table? 如何进行选择查询以从一个表中选择数据并将结果提供给另一个选择 - How to make select query to select data from one table and results give to another select 我可以从一个表中随机选择一行并使用静态标识符来选择另一张表中的相应数据吗? - Can I randomly select a row from one table and use a static identifier to select corresponding data in another table? 在一列上对数组数据进行分组,并对另一列中的数据求和 - Group array data on one column and sum data from another column 如何从一个表中选择数据并从另一表中获取所有相关记录? - How to select data from one table and get all associated records from another table? 在另一个表中的日期之间对表中的数据求和 - Sum data from a table between dates in another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM