Mysql Count of rows not referenced in other table

Zalaboza

using mysql, i keep track of if user saw post or not yet in other table

Table Posts:
id body
1  test1 

Table user_views
post_id user_id
1       1

rows above means that user 1 already saw post 1

my question is how can i get count of posts user did not see -count of posts that has how rows in user_views ?

Process: every time user open blog page i run

select count(uv.post_id) as views_count,posts.id,posts.body 
from posts left join user_views uv on uv.post_id = posts.id
group by posts.id

This return all posts along with count of how many user saw this post already

yet i want to show count of posts that the user did not see yet on sidebar so that user can know that there is new posts that he didn't see yet.

my first attempt was

select count(*) from posts 
where posts.id not in select post_id from user_views 
where post_id = posts.id and user_id = 1

yet its not valid mysql, and i doubt its the best way to do it!

e4c5

A LEFT JOIN (or for that matter a right join) with an IS NULL check is the way to go.

SELECT COUNT(*) FROM Post as posts
 LEFT JOIN user_views 
  ON posts.id = user_views.post_id 
   WHERE user_id = 1 AND user_views.post_id IS NULL

I notice that you aleady have a LEFT JOIN for a different purpose. I do believe that it can be changed to INNER JOIN to get a performance boost.

Also note that in mysql when you are using a subquery, you need to sorround it with brackets to avoid a syntax error. The corrected query will achieve the same result as what I have given. Which will be better will depend on your indexes.

select count(*) from Post as posts
where posts.id not in (select post_id from user_views 
where post_id = posts.id and user_id = 1)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Mysql Count of rows not referenced in other table

From Dev

In mysql how do I find rows whose id are not referenced in any other rows in the table?

From Dev

Mysql join table and count rows

From Dev

Count (*) rows based on values from other table

From Dev

Getting the count of rows in each category in a MySQL table

From Dev

How to count certain rows in a MySQL table?

From Dev

MySQL - Join & Count rows from another table

From Dev

Getting the count of rows in each category in a MySQL table

From Dev

How to count some rows of a MySQL table

From Dev

count rows in one table based on the id's of the other table

From Dev

mysql need get count of dependencies of other table

From Dev

mysql: Select all rows and compare with other table

From Dev

MySQL - Count rows with equal values but only if name occurs in other column

From Dev

MySQL: remove rows from table if exist in other table with condition

From Dev

Wordpress function/plugin to count number of rows in mySQL table

From Dev

Mysql update all rows value with count of same table column

From Dev

Wordpress function/plugin to count number of rows in mySQL table

From Dev

How to count number of rows in MySQL table (PHP PDO)

From Dev

mysql query to count no of rows in joining three tables and count rows of one table

From Dev

MySQL Query - SELECT row count from other table

From Dev

PostgreSQL: deleting rows referenced from another table

From Dev

How to duplicate rows of self-referenced table

From Dev

Combine rows from self-referenced table

From Dev

MySQL insert id values from other table into matching rows

From Dev

MySQL SELECT Substring of rows that do not exist from other TABLE

From Dev

mysql return matching other rows when one table returns 0

From Dev

MYSQL: How to insert rows in a table based on a condition of other two tables

From Dev

Database: Count rows in a table

From Dev

SQL count rows in a table

Related Related

  1. 1

    Mysql Count of rows not referenced in other table

  2. 2

    In mysql how do I find rows whose id are not referenced in any other rows in the table?

  3. 3

    Mysql join table and count rows

  4. 4

    Count (*) rows based on values from other table

  5. 5

    Getting the count of rows in each category in a MySQL table

  6. 6

    How to count certain rows in a MySQL table?

  7. 7

    MySQL - Join & Count rows from another table

  8. 8

    Getting the count of rows in each category in a MySQL table

  9. 9

    How to count some rows of a MySQL table

  10. 10

    count rows in one table based on the id's of the other table

  11. 11

    mysql need get count of dependencies of other table

  12. 12

    mysql: Select all rows and compare with other table

  13. 13

    MySQL - Count rows with equal values but only if name occurs in other column

  14. 14

    MySQL: remove rows from table if exist in other table with condition

  15. 15

    Wordpress function/plugin to count number of rows in mySQL table

  16. 16

    Mysql update all rows value with count of same table column

  17. 17

    Wordpress function/plugin to count number of rows in mySQL table

  18. 18

    How to count number of rows in MySQL table (PHP PDO)

  19. 19

    mysql query to count no of rows in joining three tables and count rows of one table

  20. 20

    MySQL Query - SELECT row count from other table

  21. 21

    PostgreSQL: deleting rows referenced from another table

  22. 22

    How to duplicate rows of self-referenced table

  23. 23

    Combine rows from self-referenced table

  24. 24

    MySQL insert id values from other table into matching rows

  25. 25

    MySQL SELECT Substring of rows that do not exist from other TABLE

  26. 26

    mysql return matching other rows when one table returns 0

  27. 27

    MYSQL: How to insert rows in a table based on a condition of other two tables

  28. 28

    Database: Count rows in a table

  29. 29

    SQL count rows in a table

HotTag

Archive