PHP SQL Query: how to select same Ids?

515

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] http://i.stack.imgur.com/z1Jvc.png

855

Answer

Solution:

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

People are also looking for solutions to the problem: php - Merge two 2d arrays on shared value from different keys

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.