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

时间:2013-02-04 20:12:44

标签: mysql count

所以我有4个表:用户,帖子,私人,线程。在这个例子中,lizzy在不同的线程中创建了2个私人帖子:

'约会'帖子是针对用户2,5,6和她自己查看该帖子中正确的帖子数。

'Break Ups'帖子仅供用户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_count,相反,只有lizzy和她授权查看某个帖子上某些帖子的任何用户应该看到正确的可见非私有计数他们。将计数作为一行(post_count_if_not_authorized)提取出来的最有效方法是什么,以便我们可以从post_count中减去它,以便向每个用户显示它们的正确计数?

像下面这样的东西就是我所追求的(当然不是在工作):

SELECT DISTINCT t.thread_id, t.post_count, t.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 t.user_id='3'
    AND (p.private = 0) OR (pv.authorized_user_id = {$logged_in_id} and p.private = 1)

更新

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

这是fiddle

如果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

如果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            3
    3            2

(注意:users表旁边的右上角部分显示了这些数字的派生方式。)

2 个答案:

答案 0 :(得分:2)

SELECT 
t.thread_id, t.post_count, 
COUNT(IF(ISNULL(pv.private_id) AND p.private='1' AND p.user_id<>'1', null, 1)) 
FROM threads as t
JOIN posts as p on p.thread_id = t.thread_id 
JOIN users as u on u.user_id = p.user_id 
LEFT JOIN private as pv on pv.post_id = p.post_id AND pv.authorized_user_id='1' 
JOIN users as auth on auth.user_id = '1'
WHERE p.user_id='3' AND t.user_id='3'
GROUP BY t.thread_id;

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

答案 1 :(得分:0)

如果您使用正确的group by语法,它是否有效?

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