[英]Sum row contents from a joined table
I have two tables here. 我这里有两张桌子。
log
(stands for logbook) log
(代表日志) act
(stands for actions) act
(代表行动) log 日志
contains columns: logid
, userid
, actid
包含列: logid
, userid
, actid
act 法案
contains columns: actid
, amount
包含列: actid
, amount
Amount stands for an amount of points (eg 50 of 700 or 5) the user ( userid
) receives when the corresponding actid
is inserted in the logbook ( log
). 数量代表当将相应的actid
插入日志( log
)时用户( userid
)获得的点数(例如700或5的50)。
What I'm trying to achieve is that when you search for userid
1, that all rows from log
where userid = 1
are selected. 我想要实现的是,当您搜索userid
1时,将选择log
中userid = 1
所有行。 Then, from these selected log
-rows, the actid
s should be looked up in the table act
, and the amount
s should be summed. 然后,从这些选择的log
-rows的actid
S的关系可以在表中查找act
,并amount
S的关系来概括。
In short: 简而言之:
select log
-rows where userid = 1
选择log
-rows,其中userid = 1
take all the actids
from these rows 从这些行中取出所有的actids
find the matching amount
in the act
table 在act
表中找到匹配amount
sum all these amounts
将所有这些amounts
相加
I started with this query: 我从以下查询开始:
SELECT log.logid, log.userid, log.actid, act.actid, act.amount
FROM log
JOIN act
ON act.actid = log.actid
WHERE log.userid = '1'
This got me a table with (among other things) 这给了我一张桌子(还有其他东西)
userid | 用户名| amount 量
---1----|--20--- --- 1 ---- | --20 ---
---1----|--40--- --- 1 ---- | --40 ---
---1----|---5--- --- 1 ---- | --- 5 ---
---1----|--10--- --- 1 ---- | --10 ---
Now I would like to sum
all these amounts and echo the total, but unfortunately I can't find a working query for this. 现在,我想对所有这些金额sum
并回显总计,但是不幸的是,我无法为此找到有效的查询。
EDIT: 编辑:
I used the query that Naruto provided me to sum these amounts. 我使用了鸣人提供给我的查询来汇总这些金额。 This works like a charm now! 现在,它就像一个魅力! The query: 查询:
SELECT log.userid, sum(act.amount) FROM log JOIN act ON act.actid = log.actid WHERE log.userid = '1'
The next thing that I'm trying is to get not only the summed amount from the user with userid = 1
, but also from the other users in the user
-table. 我要尝试的下一件事情不仅是从userid = 1
的用户获取总金额,而且从user
-table中的其他用户获取总金额。 After that, I'd like to use these summed amounts in the following query: 之后,我想在以下查询中使用这些总金额:
$result = $mysqli->query("SELECT * FROM `db`.`user` ORDER BY `summed_amount` DESC");
In which summed_amount
is the summed amount generated by Naruto's query. 其中summed_amount
是Naruto查询生成的总和。
MySQL has an Aggregate function called sum() which can sum all the records. MySQL有一个称为sum()的聚合函数,可以对所有记录求和。
So your query will be 所以您的查询将是
SELECT log.userid, sum(act.amount) FROM log JOIN act ON act.actid = log.actid WHERE log.userid = '1'
http://dev.mysql.com/doc/refman/5.7/en/group-by-functions.html#function_sum http://dev.mysql.com/doc/refman/5.7/zh-CN/group-by-functions.html#function_sum
To get sum of all users by a single query use group by clause.Here it will calculate sum of amaount for each user and return the result. 要通过单个查询获取所有用户的总和,请使用group by子句,此处将为每个用户计算amaount的总和并返回结果。
SELECT log.userid, sum(act.amount) FROM log JOIN act ON act.actid = log.actid group by log.userid
Edit:- To get order by desc 编辑:-要通过desc获得订单
SELECT log.userid, sum(act.amount) FROM log JOIN act ON act.actid = log.actid group by log.userid order by sum(act.amount) desc SELECT log.userid,sum(act.amount)从日志JOIN起on act.actid = log.actid按log.userid分组,按sum(act.amount)desc
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.