草庐IT

MySQL - 私有(private)帖子显示正确计数

coder 2023-10-23 原文

所以我有 4 个表:users、posts、private、threads。在此示例中,lizzy 在不同的线程中创建了 2 个私有(private)帖子:

“约会”帖子供用户 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 和她授权查看线程上某些帖子的任何用户应该看到正确的可见非私有(private)计数他们。将计数提取为一行 (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

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

最佳答案

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 相同的值。您可以完全消除线程表的使用并在查询中一起进行计数,尽管查询会更重。

关于MySQL - 私有(private)帖子显示正确计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14694774/

有关MySQL - 私有(private)帖子显示正确计数的更多相关文章

  1. ruby - 为什么我可以在 Ruby 中使用 Object#send 访问私有(private)/ protected 方法? - 2

    类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc

  2. ruby - 具有身份验证的私有(private) Ruby Gem 服务器 - 2

    我想安装一个带有一些身份验证的私有(private)Rubygem服务器。我希望能够使用公共(public)Ubuntu服务器托管内部gem。我读到了http://docs.rubygems.org/read/chapter/18.但是那个没有身份验证-如我所见。然后我读到了https://github.com/cwninja/geminabox.但是当我使用基本身份验证(他们在他们的Wiki中有)时,它会提示从我的服务器获取源。所以。如何制作带有身份验证的私有(private)Rubygem服务器?这是不可能的吗?谢谢。编辑:Geminabox问题。我尝试“捆绑”以安装新的gem..

  3. ruby-on-rails - Rails 编辑表单不显示嵌套项 - 2

    我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib

  4. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

    我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

  5. ruby-on-rails - 如何使用 instance_variable_set 正确设置实例变量? - 2

    我正在查看instance_variable_set的文档并看到给出的示例代码是这样做的:obj.instance_variable_set(:@instnc_var,"valuefortheinstancevariable")然后允许您在类的任何实例方法中以@instnc_var的形式访问该变量。我想知道为什么在@instnc_var之前需要一个冒号:。冒号有什么作用? 最佳答案 我的第一直觉是告诉你不要使用instance_variable_set除非你真的知道你用它做什么。它本质上是一种元编程工具或绕过实例变量可见性的黑客攻击

  6. ruby-on-rails - 使用 Sublime Text 3 突出显示 HTML 背景语法中的 ERB? - 2

    所以我在关注Railscast,我注意到在html.erb文件中,ruby代码有一个微弱的背景高亮效果,以区别于其他代码HTML文档。我知道Ryan使用TextMate。我正在使用SublimeText3。我怎样才能达到同样的效果?谢谢! 最佳答案 为SublimeText安装ERB包。假设您安装了SublimeText包管理器*,只需点击cmd+shift+P即可获得命令菜单,然后键入installpackage并选择PackageControl:InstallPackage获取包管理器菜单。在该菜单中,键入ERB并在看到包时选择

  7. ruby-on-rails - link_to 不显示任何 rails - 2

    我试图在索引页中创建一个超链接,但它没有显示,也没有给出任何错误。这是我的index.html.erb代码。ListingarticlesTitleTextssss我检查了我的路线,我认为它们也没有问题。PrefixVerbURIPatternController#Actionwelcome_indexGET/welcome/index(.:format)welcome#indexarticlesGET/articles(.:format)articles#indexPOST/articles(.:format)articles#createnew_articleGET/article

  8. ruby-on-rails - 如何在 Rails View 上显示错误消息? - 2

    我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c

  9. ruby-on-rails - 正确的 Rails 2.1 做事方式 - 2

    question的一些答案关于redirect_to让我想到了其他一些问题。基本上,我正在使用Rails2.1编写博客应用程序。我一直在尝试自己完成大部分工作(因为我对Rails有所了解),但在需要时会引用Internet上的教程和引用资料。我设法让一个简单的博客正常运行,然后我尝试添加评论。靠我自己,我设法让它进入了可以从script/console添加评论的阶段,但我无法让表单正常工作。我遵循的其中一个教程建议在帖子Controller中创建一个“评论”操作,以添加评论。我的问题是:这是“标准”方式吗?我的另一个问题的答案之一似乎暗示应该有一个CommentsController参

  10. ruby - 我可以将我的 README.textile 以正确的格式放入我的 RDoc 中吗? - 2

    我喜欢使用Textile或Markdown为我的项目编写自述文件,但是当我生成RDoc时,自述文件被解释为RDoc并且看起来非常糟糕。有没有办法让RDoc通过RedCloth或BlueCloth而不是它自己的格式化程序运行文件?它可以配置为自动检测文件后缀的格式吗?(例如README.textile通过RedCloth运行,但README.mdown通过BlueCloth运行) 最佳答案 使用YARD直接代替RDoc将允许您包含Textile或Markdown文件,只要它们的文件后缀是合理的。我经常使用类似于以下Rake任务的东西:

随机推荐