0

My code is:

$vote = DB::SELECT(DB::RAW("SELECT * FROM votes WHERE product_id = product_id; "));

Here[1] is a screenshot of my DB.

I'd like to ask how to select the same Producut Id in the SQL Query?

[1] https://i.sstatic.net/z1Jvc.png

5
  • SELECT * FROM votes WHERE product_id = 5? Commented Oct 20, 2015 at 19:22
  • What do you mean "the same" as what??? Commented Oct 20, 2015 at 19:22
  • Producut Id the same as what??? Commented Oct 20, 2015 at 19:23
  • @u_mulder Would like to count all the votes. So I'm going to get all the same product ids Commented Oct 20, 2015 at 19:24
  • SELECT SUM(vote) FROM votes WHERE product_id = 5 Commented Oct 20, 2015 at 19:25

1 Answer 1

1

If you want to count sum of votes for each product and sort sum of votes from highest to lowest, then your query is:

SELECT 
    product_id, SUM(vote) as `vote_rate` 
FROM 
    votes 
GROUP BY 
    product_id 
ORDER BY 
    `vote_rate` DESC
Sign up to request clarification or add additional context in comments.

Comments

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.