I have two SQL tables.
In the first table, each line has (amongst other fields that are irrelevant to the question) a score
and a category_id
field
The second table (categories
) is a table listing all the possible categories to which an element in the first table can belong.
I'd like to do the following SQL request :
SELECT category_name, ( ??? ) AS category_score
FROM categories
ORDER BY category_score DESC
where ???
= the sum of the scores of all the elements in table 1 that belong to the category
.
You could join
and group by
:
SELECT category_name, SUM(score) AS category_score
FROM categories c
JOIN element e ON c.category_id = e.category_id
GROUP BY category_name
ORDER BY 2 DESC
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments