MySQL - 错误的价值

时间:2014-02-01 14:20:21

标签: mysql sql

我尝试计算一些数字并且我一直发誓因为它失败然后我尝试了这个:

SELECT SUM( 10 *1 ) 
FROM user_achievements
INNER JOIN achievements
WHERE user_achievements.user_id =8

它说:420?!?

我尝试让这个工作:

SELECT SUM((SELECT score_base FROM achievements WHERE id = user_achievements.achievement_id)*((SELECT pixels_multiplier FROM achievements WHERE id = user_achievements.achievement_id)) * achievement_level) * achievement_level FROM user_achievements INNER JOIN achievements WHERE user_achievements.user_id=2

成就: ID, 水平, pixels_base, score_base, pixels_multiplier

user_achievements: 用户身份, achievement_id, achievement_level

1 个答案:

答案 0 :(得分:2)

如果您的查询返回420,则表示在聚合之前在结果集中返回42行。

这是您的查询:

SELECT sum(10*1)
FROM user_achievements ua cross join
     achievements a
WHERE ua.user_id = 8;

如果我不得不猜测,你错过了一个加入:

SELECT sum(10*1)
FROM user_achievements ua join
     achievements a
     on ua.achievement_id = a.id
WHERE ua.user_id = 8;

编辑:

这是评论中的查询:

SELECT sum((SELECT score_base
            FROM achievements
            WHERE id = ua.achievement_id
            ) *
            (SELECT pixels_multiplier
             FROM achievements
             WHERE id = ua.achievement_id
            )
           )
FROM user_achievements ua join
     achievements a
     on ua.achievement_id = a.id
WHERE ua.user_id = 2;

它甚至不应该解析。聚合中不允许子查询。试试这个:

SELECT sum(a.score_base * a.pixels_multiplier)
FROM user_achievements ua join
     achievements a
     on ua.achievement_id = a.id
WHERE ua.user_id = 2;