3

I have a mysql table with actions and question_ids. Each action comes with a score like this:

     ACTION       | SCORE
downvote_question |  -1
upvote_question   |  +1
in_cardbox        |  +2

I want to query for the question with the highest score but I can't figure it out.

http://sqlfiddle.com/#!2/84e26/15

So far my query is:

SELECT count(*), l1.question_id, l1.action 
FROM `log` l1
GROUP BY l1.question_id, l1.action

which gives me every question_id with all its accumulated actions.

What I want is this:

QUESTION_ID | SCORE
     2      |   5
     1      |   4
     3      |   1
     4      |   1
     5      |   1

I can't figure it out - I probably need subqueries, JOINS or UNIONS...

3 Answers 3

2

Maybe you can try this one.

SELECT  a.question_id, sum(b.score) totalScore
FROM   `log` a INNER JOIN scoreValue b
          on a.`action` = b.actionname
group by a.question_id
ORDER BY totalScore DESC

SQLFiddle Demo

Sign up to request clarification or add additional context in comments.

5 Comments

that's pretty nice already except that the scores come from php constants and not another table. should be easy enough to adjust though. i'll give it a try
you can create table for your constants though :)
I could - but that would mean, I would have to adjust scores in two places whenever scores would be changed - so I'd prefer storing them in one place.
it is better to store them in db, and just fetch them from php on each load. cuz storing it in future in your php file it's not a good idea.
@Horen you would have more work to do if the constants are on your PHP file. but it still depends within you :)
1

You should replace count(*) with sum(l1.score) because sum will add all values based on group by statement

SELECT sum(l1.score), l1.question_id, l1.action 
FROM `log` l1
GROUP BY l1.question_id, l1.action

With constant scores works on SQL Fiddle (with grouping by question):

SELECT 
sum(
  CASE WHEN l1.action = 'downvote_question' THEN -1 
  WHEN l1.action = 'upvote_question' THEN 1 
  ELSE 2 END
) score,
l1.question_id
FROM `log` l1
GROUP BY l1.question_id

4 Comments

there is no such column as score
but score is based on which criteria on action value?
yes. the score comes from php constants. so they have to be in the query already.
try now, it doesn't need grouping by action but only by question to get total score. I forgot to add comparison in case because all values was equals to 2 without this
0
SELECT sum(SCORE), l1.question_id, l1.action 
FROM `log` l1
GROUP BY l1.question_id, l1.action

is it what you want to?

upd: in your code on fidel, there is no such column as score, but i think it wont be a problem to create a tabel with action | score and join it to sum(score)

1 Comment

the score comes from php constants and has to be added to the query. For now we could hard code it

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.