Getting table rows in order of sum of count of two another columns from another table

AnkitJ

I have 3 MYSQL tables they are:

POST

mysql> DESCRIBE `posts`;
+------------------+------------------+------+-----+---------+----------------+
| Field            | Type             | Null | Key | Default | Extra          |
+------------------+------------------+------+-----+---------+----------------+
| post_id          | int(11) unsigned | NO   | PRI | NULL    | auto_increment |
| post_details     | varchar(11)      | NO   |     | NULL    |                |                |
+------------------+------------------+------+-----+---------+----------------+

Likes

mysql> DESCRIBE `likes`;
+------------------+------------------+------+-----+---------+----------------+
| Field            | Type             | Null | Key | Default | Extra          |
+------------------+------------------+------+-----+---------+----------------+
| id               | int(11) unsigned | NO   | PRI | NULL    | auto_increment |
| post_id          | int(11)          | NO   |     | NULL    |                |
| user_id          | int(11)          | NO   |     | NULL    |                |                |
+------------------+------------------+------+-----+---------+----------------+

Comments

mysql> DESCRIBE `comments`;
+------------------+------------------+------+-----+---------+----------------+
| Field            | Type             | Null | Key | Default | Extra          |
+------------------+------------------+------+-----+---------+----------------+
| id               | int(11) unsigned | NO   | PRI | NULL    | auto_increment |
| post_id          | int(11)          | NO   |     | NULL    |                |
| user_id          | int(11)          | NO   |     | NULL    |                |                |
+------------------+------------------+------+-----+---------+----------------+

In 2nd and 3rd table, I am storing likes and comments of posts( 1st table ). Now, I want to get posts in descending order of sum of count of likes and comments. i.e. posts will be in order of maximum number of likes + comments to minimum number. Please help me to get right result and thanks in advance.

Gianmarco
SELECT posts.post_id, post_details,
    ((SELECT(COUNT(id) FROM comments WHERE comments.post_id = posts.post_id)) + (SELECT(COUNT(id) FROM likes WHERE likes.post_id = posts.post_id))) AS weight
FROM posts
ORDER BY weight DESC

Without using variables you can use nested selects

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to dynamically count rows in a table from two another tables?

From Dev

How to count rows from another table to affect another table

From Dev

Select * as well as count/sum from another table

From Dev

MySQL - Join & Count rows from another table

From Dev

SQL COUNT Rows from another table

From Dev

Join a columns count from another table

From Dev

Getting a value from another table for multiple columns

From Dev

mysql: select rows from another table as columns

From Dev

Merging two columns with values from another table

From Dev

MySQL Order by sum of another table

From Dev

Oracle Temporary table columns and data from another table's rows

From Dev

Not getting sum of rows from table

From Dev

How to use value from two different rows of a table in another table

From Dev

Display 2 columns from one table having max count in column 3 and display computed sum of values from another table

From Dev

Select records from a table where two columns are not present in another table

From Dev

Delete rows in MySQL matching two columns in another table

From Dev

Delete rows in MySQL matching two columns in another table

From Java

How to fast return and count rows from one table based on filter by two another tables

From Dev

Update sum from another table

From Dev

Count number of rows from another table in single query

From Dev

SQLite: populate table with count of matching rows from another

From Dev

How to count rows that got copied from one Hive table to another

From Dev

Count rows from another SQL table in the same query

From Dev

SQL Hide/Show rows based on row count from another table

From Dev

How to make an efficient Count of rows from another table (join)

From Dev

Getting rows that contain a value from another table SAS

From Dev

MySQL - How do I order based on columns from another table

From Dev

Count Row from another table

From Dev

Filter by count from another table

Related Related

  1. 1

    How to dynamically count rows in a table from two another tables?

  2. 2

    How to count rows from another table to affect another table

  3. 3

    Select * as well as count/sum from another table

  4. 4

    MySQL - Join & Count rows from another table

  5. 5

    SQL COUNT Rows from another table

  6. 6

    Join a columns count from another table

  7. 7

    Getting a value from another table for multiple columns

  8. 8

    mysql: select rows from another table as columns

  9. 9

    Merging two columns with values from another table

  10. 10

    MySQL Order by sum of another table

  11. 11

    Oracle Temporary table columns and data from another table's rows

  12. 12

    Not getting sum of rows from table

  13. 13

    How to use value from two different rows of a table in another table

  14. 14

    Display 2 columns from one table having max count in column 3 and display computed sum of values from another table

  15. 15

    Select records from a table where two columns are not present in another table

  16. 16

    Delete rows in MySQL matching two columns in another table

  17. 17

    Delete rows in MySQL matching two columns in another table

  18. 18

    How to fast return and count rows from one table based on filter by two another tables

  19. 19

    Update sum from another table

  20. 20

    Count number of rows from another table in single query

  21. 21

    SQLite: populate table with count of matching rows from another

  22. 22

    How to count rows that got copied from one Hive table to another

  23. 23

    Count rows from another SQL table in the same query

  24. 24

    SQL Hide/Show rows based on row count from another table

  25. 25

    How to make an efficient Count of rows from another table (join)

  26. 26

    Getting rows that contain a value from another table SAS

  27. 27

    MySQL - How do I order based on columns from another table

  28. 28

    Count Row from another table

  29. 29

    Filter by count from another table

HotTag

Archive