SQL: how to select something using information from another table

François M.

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.

Mureinik

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Insert in SQL table using select with column values from another select

From Dev

How to select the information from the table?

From Dev

How to get the information from table to another page using PHP and MYSQL?

From Dev

SQL: How to SELECT different values from a table in another table?

From Dev

Sql how to select from a table that does not exist in another table

From Dev

SQL: How to SELECT different values from a table in another table?

From Dev

How to get an avarage of something from a table using data from another table

From Dev

How to insert into table from another table using conditional select

From Dev

How to select using associated records and sql avg function from another table?

From Dev

select from table and update another table SQL

From Dev

SQL - How to select a column alias from another table?

From Dev

SQL: How select rows and minus from another table

From Dev

SQL how to select user that is in all lines from another table

From Dev

How to select from one dbms_sql.number_table into another

From Dev

How to make a query to SELECT something based on results from another SELECT?

From Dev

How to get name from another table when using a key in SELECT?

From Dev

how to select photos from another table using mysql,php

From Dev

Mysql DELETE from table using information from another table

From Dev

Select from a table based on a regex split from another table using Oracle SQL

From Dev

Sql Select by using another table value(s)

From Dev

Sql Select by using another table value(s)

From Dev

Get information from another table using intermediate table

From Dev

How to get information from SQL table with php

From Dev

SQL: How to build a select query with Table_Name and Pivoted Column_Name from Information Schema

From Dev

MS Access ?: How to pull information into a table from another linked table

From Dev

How do i randomly select something from a table?

From Dev

Retrieving data information from another table Left Join SQL

From Dev

How to select information from more then one table in MySQL Database using PHP and mySQli, has to be combined single query

From Dev

How do I select rows in 1 table containing keywords from another table in SQL?

Related Related

  1. 1

    Insert in SQL table using select with column values from another select

  2. 2

    How to select the information from the table?

  3. 3

    How to get the information from table to another page using PHP and MYSQL?

  4. 4

    SQL: How to SELECT different values from a table in another table?

  5. 5

    Sql how to select from a table that does not exist in another table

  6. 6

    SQL: How to SELECT different values from a table in another table?

  7. 7

    How to get an avarage of something from a table using data from another table

  8. 8

    How to insert into table from another table using conditional select

  9. 9

    How to select using associated records and sql avg function from another table?

  10. 10

    select from table and update another table SQL

  11. 11

    SQL - How to select a column alias from another table?

  12. 12

    SQL: How select rows and minus from another table

  13. 13

    SQL how to select user that is in all lines from another table

  14. 14

    How to select from one dbms_sql.number_table into another

  15. 15

    How to make a query to SELECT something based on results from another SELECT?

  16. 16

    How to get name from another table when using a key in SELECT?

  17. 17

    how to select photos from another table using mysql,php

  18. 18

    Mysql DELETE from table using information from another table

  19. 19

    Select from a table based on a regex split from another table using Oracle SQL

  20. 20

    Sql Select by using another table value(s)

  21. 21

    Sql Select by using another table value(s)

  22. 22

    Get information from another table using intermediate table

  23. 23

    How to get information from SQL table with php

  24. 24

    SQL: How to build a select query with Table_Name and Pivoted Column_Name from Information Schema

  25. 25

    MS Access ?: How to pull information into a table from another linked table

  26. 26

    How do i randomly select something from a table?

  27. 27

    Retrieving data information from another table Left Join SQL

  28. 28

    How to select information from more then one table in MySQL Database using PHP and mySQli, has to be combined single query

  29. 29

    How do I select rows in 1 table containing keywords from another table in SQL?

HotTag

Archive