Selecting rows from one table where value and order from another in MYSQL

kubizen

I have two tables:

post:

id    text
1     abc
2     abcd
3     bcd

and voting:

post_id  vote
2         2
1         5
3         1
1         3

I want to show top 10 posts by rating in "votes" table:

SELECT * FROM post
WHERE id IN
    (SELECT post_id FROM
        (SELECT SUM(vote) as totalvote, post_id
        FROM voting
        GROUP BY post_id) as table1
    ORDER BY totalvote DESC)
LIMIT 10"

but it's order by post id. How can i order it by total votes?

Tomaso Albinoni

Probably this is what you need:

SELECT post.id, post.text FROM post
JOIN voting ON voting.post_id = post.id
GROUP BY post.id
ORDER BY AVG(vote) DESC
LIMIT 10

At least I think it would make sense to order by average rating. But of course you can order by total points given:

SELECT post.id, post.text FROM post
JOIN voting ON voting.post_id = post.id
GROUP BY post.id
ORDER BY SUM(vote) DESC
LIMIT 10

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Efficiently deleting rows from one table where not matching another [MySQL]

From Dev

selecting unique rows from one table according to another table and then sorting it

From Dev

Selecting two rows from another table using one row

From Dev

mysql - move rows from one table to another

From Dev

MySQL - Selecting data from another table for a WHERE clause

From Dev

Inserting all rows one by one into a empty table by selecting the rows from another table in SQL Server

From Dev

Selecting data from one table based on the value of another

From Dev

Selecting rows that are included in a set from another table

From Dev

php selecting an order from a different mysql table

From Dev

MySQL - Exclude all rows from one table if match on another table

From Dev

Mysql Trigger, Copy All Rows From One Table Into Another Table

From Dev

mysql selecting data from one table based on data from another table

From Dev

Selecting a Diff report from one table MySQL

From Dev

Copy only specific rows from one table to another in PHP and MySQL

From Dev

Insert data from a table to another one in MySQL with where condition

From Dev

Transferring data from one table to another using a where clause mysql

From Dev

Selecting a table and filtering by several values from one column as well as a value from another

From Dev

Selecting rows from a table depending on keywords in another table

From Dev

MySQL Count values from one column with selecting a value from another column

From Dev

Subtract rows of one table from another table

From Dev

Selecting rows that are within a date range based on a date from another table. (MYSQL)

From Dev

Selecting data from table where sum of values in a column equal to the value in another column

From Dev

select row from one table where value exists from array from another table

From Dev

Mysql rows from table A with ID from query where ID matches one condition true in table B

From Dev

MySQL query most recent records from one table but in the order from another table

From Dev

Selecting rows absent from one table against two other tables?

From Dev

Adding rows from one table to another existing table where primary key is autogenerated

From Dev

Adding rows from one table to another existing table where primary key is autogenerated

From Dev

How to copy rows from one table to another?

Related Related

  1. 1

    Efficiently deleting rows from one table where not matching another [MySQL]

  2. 2

    selecting unique rows from one table according to another table and then sorting it

  3. 3

    Selecting two rows from another table using one row

  4. 4

    mysql - move rows from one table to another

  5. 5

    MySQL - Selecting data from another table for a WHERE clause

  6. 6

    Inserting all rows one by one into a empty table by selecting the rows from another table in SQL Server

  7. 7

    Selecting data from one table based on the value of another

  8. 8

    Selecting rows that are included in a set from another table

  9. 9

    php selecting an order from a different mysql table

  10. 10

    MySQL - Exclude all rows from one table if match on another table

  11. 11

    Mysql Trigger, Copy All Rows From One Table Into Another Table

  12. 12

    mysql selecting data from one table based on data from another table

  13. 13

    Selecting a Diff report from one table MySQL

  14. 14

    Copy only specific rows from one table to another in PHP and MySQL

  15. 15

    Insert data from a table to another one in MySQL with where condition

  16. 16

    Transferring data from one table to another using a where clause mysql

  17. 17

    Selecting a table and filtering by several values from one column as well as a value from another

  18. 18

    Selecting rows from a table depending on keywords in another table

  19. 19

    MySQL Count values from one column with selecting a value from another column

  20. 20

    Subtract rows of one table from another table

  21. 21

    Selecting rows that are within a date range based on a date from another table. (MYSQL)

  22. 22

    Selecting data from table where sum of values in a column equal to the value in another column

  23. 23

    select row from one table where value exists from array from another table

  24. 24

    Mysql rows from table A with ID from query where ID matches one condition true in table B

  25. 25

    MySQL query most recent records from one table but in the order from another table

  26. 26

    Selecting rows absent from one table against two other tables?

  27. 27

    Adding rows from one table to another existing table where primary key is autogenerated

  28. 28

    Adding rows from one table to another existing table where primary key is autogenerated

  29. 29

    How to copy rows from one table to another?

HotTag

Archive