0

I try calculate some numbers and i sweared alltime because it failing and then i tryed this:

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

and it says its: 420 ?!?

I try make this working:

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

achievements: id, levels, pixels_base, score_base, pixels_multiplier

user_achievements: user_id, achievement_id, achievement_level

2
  • Can you show the create statements for the two tables. You can leave out any fields not used. Commented Feb 1, 2014 at 15:32
  • Added! Hope it helps :) Commented Feb 1, 2014 at 15:43

1 Answer 1

2

If your query is return 420, that means that 42 rows are returned in the result set before the aggregation.

This is your query:

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

If I had to guess, you are missing a join:

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

EDIT:

This is the query in the comment:

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;

It shouldn't even parse. Subqueries are not allowed in aggregations. Try this:

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;
Sign up to request clarification or add additional context in comments.

7 Comments

#1054 - Unknown column 'a.achievement_id' in 'on clause'
Just change the a to ua and the ua to a in that comparison
@user3249998 . . . I don't know the names of the fields in your table. I was showing an example of what the query would look like. Use it as a guide given your own database.
I tryed this and not working: SELECT sum((SELECT score_base FROM achievements WHERE id = user_achievements.achievement_id) *(SELECT pixels_multiplier FROM achievements WHERE id = user_achievements.achievement_id)) FROM user_achievements user_achievements join achievements a on user_achievements.achievement_id = a.id WHERE user_achievements.user_id = 2;
Both give this error: #1054 - Unknown column 'user_achievements.user_id' in 'where clause'
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.