MySQL join / sum value from one table colum with a count values from another

user3403520

Table 1 - "news"

--------------------------------------
|news_id|news_title|fb_comments_count|
--------------------------------------
|     1|        XYZ|                0|
|     2|        ABC|               32|
|     3|        DEF|                5|
--------------------------------------

Table 2 - "news_comments"

------------------------------
|com_id|   news_id|   comment|
------------------------------
|     1|         1| blablabla|
|     2|         2| test     |
|     3|         1| comment  |
|     4|         1| asdf     |
------------------------------

I need to get total comments sum for every single news, where facebook comments count from the first table and total count of comments for every specific news from the other table, are counted together.

Like this - "Total comments":

-----------------------------------
|   id | news_id  | total comments (fb_comments + count of comments from comments table)
-----------------------------------
|     1|        2 |             33|
|     2|        3 |              5|
|     3|        1 |              3|
-----------------------------------

Thanks in advance! :)

wvdz

A simple join and a group by can count all your comments with COUNT(). Then simply add this to the fb_comment_count. We use a LEFT JOIN to make sure it also includes news_id's that don't occur in news_comments, and apply the COUNT() only to c.com_id, to ensure this will give 0 for these situations when there are no news_comments for this news_id.

SELECT n.news_id, COUNT(c.com_id) + n.fb_comments_count AS total_comments
FROM news n
LEFT OUTER JOIN news_comments c ON c.news_id = n.news_id
GROUP BY n.news_id, n.fb_comments_count

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Join a columns count from another table

From Dev

MySQL query with COUNT and join column from another table

From Dev

mySQL SELECT from one table, JOIN from another, then SELECT from the new one

From Dev

Selecting SUM+COUNT from one table and COUNT from another in Single query

From Dev

join count from one table to select from another - mysql

From Dev

Select values from one table depending on referenced value in another table

From Dev

Return Sum from another table in join with duplicates

From Dev

MySQL count of count, use result from one table with another

From Dev

MySQL: Is there a way to insert values from one table to another?

From Dev

Joining row from one table with the sum value from another table

From Dev

How can I get value from one table and array of values from another join table in one mysql query?

From Dev

MYSQL join one colum from one table to two count() in another

From Dev

MySQL query to sum one column and count another column, from two tables, based on a common value?

From Dev

MySQL set column value from another table (JOIN 3 tables)

From Dev

MySQL - Join & Count rows from another table

From Dev

mysql insert unique values from one column to a column of another table

From Dev

Retriving the count from one table given a value from another

From Dev

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

From Dev

Mysql and php sum of values from colum depending on date

From Dev

Join multiple values from one column, selected from another table

From Dev

MySQL: Is there a way to insert values from one table to another?

From Dev

Select from one table and count from another

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 * as well as count/sum from another table

From Dev

PHP PDO count and sum value from MySQL table

From Dev

MySQL query - select from one, count from another table

From Dev

how to show 2 value and one values count from 3 table using mysql

From Dev

Subtract total sum of a colum, From the total sum of a colum from another table

From Dev

Join in MYSQL (Count from One table and list from one table)

Related Related

  1. 1

    Join a columns count from another table

  2. 2

    MySQL query with COUNT and join column from another table

  3. 3

    mySQL SELECT from one table, JOIN from another, then SELECT from the new one

  4. 4

    Selecting SUM+COUNT from one table and COUNT from another in Single query

  5. 5

    join count from one table to select from another - mysql

  6. 6

    Select values from one table depending on referenced value in another table

  7. 7

    Return Sum from another table in join with duplicates

  8. 8

    MySQL count of count, use result from one table with another

  9. 9

    MySQL: Is there a way to insert values from one table to another?

  10. 10

    Joining row from one table with the sum value from another table

  11. 11

    How can I get value from one table and array of values from another join table in one mysql query?

  12. 12

    MYSQL join one colum from one table to two count() in another

  13. 13

    MySQL query to sum one column and count another column, from two tables, based on a common value?

  14. 14

    MySQL set column value from another table (JOIN 3 tables)

  15. 15

    MySQL - Join & Count rows from another table

  16. 16

    mysql insert unique values from one column to a column of another table

  17. 17

    Retriving the count from one table given a value from another

  18. 18

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

  19. 19

    Mysql and php sum of values from colum depending on date

  20. 20

    Join multiple values from one column, selected from another table

  21. 21

    MySQL: Is there a way to insert values from one table to another?

  22. 22

    Select from one table and count from another

  23. 23

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

  24. 24

    Select * as well as count/sum from another table

  25. 25

    PHP PDO count and sum value from MySQL table

  26. 26

    MySQL query - select from one, count from another table

  27. 27

    how to show 2 value and one values count from 3 table using mysql

  28. 28

    Subtract total sum of a colum, From the total sum of a colum from another table

  29. 29

    Join in MYSQL (Count from One table and list from one table)

HotTag

Archive