Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL-私有帖子显示正确的计数_Mysql_Count - Fatal编程技术网

MySQL-私有帖子显示正确的计数

MySQL-私有帖子显示正确的计数,mysql,count,Mysql,Count,所以我有4个表:用户表、帖子表、私有表和线程表。在本例中,lizzy在不同的线程中创建了两个私有帖子: “约会”帖子供用户2、5、6和她自己查看该帖子的正确数量 “分手”帖子仅供用户2和她自己查看该线程中帖子的正确数量 根据查看线程的用户显示正确的计数是我面临的问题。在这里,我们重点关注lizzy、她的帖子和帖子数量: users (These aren't part of table. Just shows the counts we should di

所以我有4个表:用户表、帖子表、私有表和线程表。在本例中,lizzy在不同的线程中创建了两个私有帖子:

“约会”帖子供用户2、5、6和她自己查看该帖子的正确数量

“分手”帖子仅供用户2和她自己查看该线程中帖子的正确数量

根据查看线程的用户显示正确的计数是我面临的问题。在这里,我们重点关注lizzy、她的帖子和帖子数量:

users                     (These aren't part of table. Just shows the counts we should display with our query depending on the user_id)
user_id |  user_name      //thread #: post_count-post_count_if_not_authorized = count to show 
--------------------
   1    |    tony         //thread 2: 3-1= 2 posts. thread 3: 2-1= 1 post.
   2    |    steph        //thread 2: 3-0= 3 posts. thread 3: 2-0= 2 posts.
   3    |    lizzy        //thread 2: 3 posts. thread 3: 2 posts.
   4    |    adam         //thread 2: 3-1= 2 posts. thread 3: 2-1= 1 post.
   5    |    lara         //thread 2: 3-0= 3 posts. thread 3: 2-1= 1 post.
   6    |    alexa        //thread 2: 3-0= 3 posts. thread 3: 2-1= 1 post.


posts
post_id   thread_id   user_id   post_name   private (0 is public, 1 is private to authorized users)
-----------------------------------------------------
   1         1           1       Coding        0
   2         2           3       Dating        1
   3         2           3       Show Me       0
   4         2           3       See Me        0 
   5         3           3       Break Ups     1
   6         3           3       True Love     0

private
private_id   post_id   authorized_user_id
-----------------------------------------------
    1           2               2
    2           2               5
    3           2               6
    4           5               2

threads
thread_id  user_id  post_count
------------------------------------
    1         1         1
    2         3         3  | When outputted in php, we should subtract the correct COUNT
    3         3         2  | from this depending on the user viewing the thread like above.
因此,基本上,我们有一个线程总数,其中包含该线程中的所有帖子。但是如果我们用mysql查询,所有用户都会看到lizzy的每个线程的所有post_计数,而只有lizzy和任何她授权查看线程上某些帖子的用户才能看到他们的正确可见非私有计数。将计数作为一行(如果未授权,则为post_计数),以便我们可以从post_计数中减去它,从而仅向每个用户显示正确的计数,最有效的方法是什么

下面是我想要的东西(当然不是像现在这样工作):

更新:

(WHERE子句中的t.user_id='3'在本例中代表lizzy,如果$logged_in_id根据用户给出正确的计数,如上面用户表中的计数)

这是一本书

如果tony($logged_in_id=1)正在查看lizzy(user_id=3)启动的线程,则输出应如下所示:

thread_id    post_count_final (this should have the correct count for this user($logged_in_id) to see, all posts by lizzy - private posts by lizzy that this user is not authorized to see)
    2            2
    3            1
thread_id    post_count_final (this should have the correct count for this user($logged_in_id) to see, all posts by lizzy - private posts by lizzy that this user is not authorized to see)
    2            3
    3            2
如果steph($logged_in_id=2)正在查看lizzy(user_id=3)启动的线程:

thread_id    post_count_final (this should have the correct count for this user($logged_in_id) to see, all posts by lizzy - private posts by lizzy that this user is not authorized to see)
    2            2
    3            1
thread_id    post_count_final (this should have the correct count for this user($logged_in_id) to see, all posts by lizzy - private posts by lizzy that this user is not authorized to see)
    2            3
    3            2

(注意:用户表旁边的右上部分显示了这些数字是如何推导出来的。)

如果使用正确的
分组方式语法,它是否有效

SELECT t.thread_id, t.post_count, COUNT(*)
FROM threads as t
JOIN posts as p on p.user_id=t.user_id 
LEFT JOIN private pv on pv.post_id=p.post_id
WHERE (p.private=0) OR (pv.authorized_user_id={$logged_in_id} and p.private=1) 
group by t.thread_id, t.post_count
选择
t、 螺纹id,t.post\U计数,
计数(如果(ISNULL(pv.private_id)和p.private='1'和p.user_id'1',null,1))
从线程作为t
在p.thread\u id=t.thread\u id上以p的形式连接帖子
以u.user\u id=p.user\u id上的u身份加入用户
在pv.post\u id=p.post\u id和pv.authorized\u user\u id='1'上以pv的形式左键连接private
在auth.user\u id='1'上以auth身份加入用户
其中p.user_id='3'和t.user_id='3'
按t.thread_id分组;

即使lizzy(已登录)正在查看lizzy,此操作也应有效。添加COUNT(*)将返回与t.post_COUNT相同的值。您可以完全取消threads表的使用,并在查询中一起进行计数,尽管查询将更加繁重。

您可以向用户解释一下表数据吗?现在格式更好了,只需一个用户id和用户名称,以便更清晰,您能将所需结果添加到上面的帖子中吗。@为了清晰起见,草莓添加了一个输出部分,显示所需结果。感谢您的快速回复。关于你的回答,我有三个问题:1。您能详细解释一下“计数(如果(ISNULL(pv.private_id)和p.private'0'和p.user_id'1',null,1))”吗?这似乎行得通,但我不明白所有的部分都是什么意思。2.如果我们使用$logged\u-in\u-id作为7,它不存在。我们仍然取得了成果。这有意义吗?另外,如果用户注销,那么$logged\u in\u id='',查询是否相同?3.如果不使用threads表,我觉得每页的计数都会太重。你认为这是最有效的解决方案吗。COUNT()将对函数内容不为null的记录进行计数。这就是IF(,null,1)的作用。2.将af连接添加到用户作为“auth”进行身份验证。如果用户未登录(包括“7”和“3”),这将返回0条记录。是的,我同意如果帖子的数量随着时间的推移会变得越来越大,那么我宁愿维护一个计数,而不是每次都查询它。为了便于可读性和逻辑性,我将if转换成这样的一个案例:count(当ISNULL(pv.private_id)和p.private='1'以及p.user_id='3'时,则为null,否则为1 end),这是正确的吗?而不是p.private'0'和p.user_id'1'(因为将有多个用户1和3),我将上面的p.private='1'和p.user_id='3'放在一起。还有,为什么它会弄乱计数id?我们在then else中使用0而不是null?我已经测试过了,我的更改也有效,但最后一个问题是它。如果我以lizzy$logged_in_id='3'的身份登录,是否有办法完全跳过sql语句中的计数操作,并为行返回自定义值,如“threader”?类似于:COUNT(如果(p.user\u id'3'然后'threader'else(case when ISNULL(pv.private\u id)和p.private='1'以及p.user\u id='3'然后null else 1 end))作为自定义的\u COUNT\u行替换为case是可以的,但是最后一个条件必须是p.user\u id{$logged\u in\u id},以防止在用户不是海报的情况下计数works可能会有点混乱,无论内容是0、1、100还是false都无关紧要,它仍然会计算记录,只有当内容为null时才会将其排除。