简体   繁体   English

如何从MySQL中的联接表中获取值的总和?

[英]How to get sum of values from joined table in MySQL?

I have two tables with values similar to the data below. 我有两个表,其值类似于下面的数据。

Scores Table      Posts Table
postId score      id   user
     1     1       1   me
     2    -1       2   you
     1     1       3   me
     2     2
     3     0

From these two tables, I'd like to calculate the score for a given user with a stored procedure that I'd call like getScoreForUser(user). 从这两个表中,我想使用给定的存储过程(如getScoreForUser(user))来计算给定用户的分数。

The results in this exact case would return 在这种情况下的结果将返回
CALL getScoreForUser(me) = 2 CALL getScoreForUser(me) = 2
CALL getScoreForUser(you) = 1 CALL getScoreForUser(you) = 1

I've tried some pretty stupid things so far: 到目前为止,我已经尝试了一些非常愚蠢的方法:

select score.scores FROM scores INNER JOIN posts ON (scores.postId=posts.id);

which is obviously not even close. 这显然还差得远。 Is a join even appropriate in this case? 在这种情况下联接是否合适?

I'm happy to add them up myself if it's easier (more efficient in Node.js than MySQL for example), though I understand that there are some arithmetic operations available in MySQL. 我很高兴自己添加它们(如果它更简单(例如,在Node.js中比MySQL更高效)),尽管我知道MySQL中有一些算术运算。

I think this should do the trick, assuming I've understood your database schema correctly: 假设我正确理解了您的数据库架构,我认为这应该可以解决问题:

SELECT id, SUM(scores.score) AS totalScore
FROM posts
JOIN scores
ON scores.postId = posts.id;

From there, you should be able to use totalScore as expected. 从那里,您应该能够按预期使用totalScore

The relevant bit here is the MySQL SUM() function , which we use to add up all the scores. 这里最重要的是MySQL SUM()函数 ,我们使用它来求和所有分数。

Your query to group all the items scores based on id will be: 您根据ID将所有项目得分分组的查询将是:

SELECT
  a.id,
  SUM(b.scores) as score
FROM
  posts as a
LEFT JOIN
  Scores as b
ON
  a.id = b.postId
GROUP BY
  a.id

You can then take that and add a where statement in for specific id's in your stored procedure 然后,您可以将其添加到存储过程中特定ID的where语句中

Ultimately, this is what I used: 最终,这是我所使用的:

CREATE PROCEDURE getScoreForUser(IN inUser VARCHAR(255))
BEGIN
    SELECT id, SUM(scores.score) AS totalScore
    FROM posts
    JOIN scores
    ON scores.postId = posts.id
    WHERE posts.user = inUser;
END

I'm using this in a Node application where we originally thought that stored procedures were the magical cure to sql injection which is why we're still using them, even though we've learned that's not quite true. 我在Node应用程序中使用它,我们最初认为存储过程是sql注入的神奇解决方案,这就是为什么我们仍然使用它们的原因,尽管我们已经知道这不是真的。

Using a stored procedure for this seems a bit of overkill for such a straightforward query. 为此使用存储过程对于这样一个简单的查询似乎有些过大。 If you already know the integer id then you don't need the JOIN. 如果您已经知道整数ID,则不需要JOIN。 However, assuming that you want to pass in the user's name the procedure would look like this: 但是,假设您要传递用户名,则过程如下所示:

DELIMITER //
DROP PROCEDURE IF EXISTS getScoreForUser //
CREATE PROCEDURE getScoreForUser(IN v_user VARCHAR(32))
BEGIN
  SELECT SUM(score) AS totalScore
  FROM posts p
  JOIN scores s
  ON (s.postId = p.id)
  WHERE p.user = v_user;
END //

DELIMITER ;

CALL getScoreForUser('me');

Also, note you have to use quotes around the name in the CALL to the procedure. 另外,请注意,您必须在过程的CALL中使用名称的引号引起来。

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

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