【问题标题】:How to query the filter result in the following scenario in MYSQL/ SQL?如何在MYSQL/SQL中查询以下场景中的过滤结果?
【发布时间】:2014-06-20 14:51:33
【问题描述】:

我正在开发一个类似于 Facebook 评论/论坛帖子的功能。

表结构是这样的:

Post
========    
id
txtMsg
createDate
updateDate
isShow
userID
postID

User
=======
id
name

所以,这个表已经代表了帖子和评论。这意味着,如果是 post , postID 将为 null ,如果这是 reply ,则它具有父 postID。

我的目标是

  1. 按创建日期 (Desc) 获取用户的评论/帖子以及他们朋友的帖子顺序

  2. 有一个字段调用 isShow,这意味着对于任何 isShow 为 0 的帖子,那么所有帖子和评论都不应包含在结果集中。

  3. 加入用户表获取用户名

我在 codeigniter 项目上工作,但这没关系,我可以转换语法 SQL 查询。非常感谢您提供任何帮助。

在我的预期结果中,我可以在我的界面中生成这样的输出

    Comment-er name (comment date)
    comment message

    1st Reply-er name (1st reply date)
    1st reply message

    2nd Reply-er name (2nd reply date)
    2nd reply message

.....

尝试这样的代码,但需要一些调整,其中 $facebookIDList 已经包含用户 ID 和用户的朋友 ID

$this->db->select('c.id as id, u.name as username, c.txtMsg as txtMsg, c.createDate as createDate, c.updateDate as updateDate, c.imageURL as imageURL, c.postID as postID, c.isShow as isShow');
$this->db->from('comment as c, user as u');
$this->db->where('u.id = c.userID');
$this->db->where('c.isShow = 1');
$this->db->where_in('c.userID', $facebookIDList);
$this->db->order_by('c.createDate', 'Desc');

【问题讨论】:

    标签: php mysql sql database codeigniter


    【解决方案1】:

    请澄清一件事。你说“所以,这个表已经代表了帖子和评论。这意味着,如果是帖子,则 postID 将为 null ,如果这是回复,则它具有父 postID。” 如果任何新帖子的 postID 为空,那么您如何回复父 postID??

    【讨论】:

    • 感谢您的回复。对于帖子,例如ID : 5, POSTID : NULL, 回复它,例如ID:6,POSTID:5。
    • 我认为你应该使用不同的表来发布和 cmets/reply。两者都将通过 userID 和 post ID 连接。新的帖子 ID 将是 replyID,在您的情况下是 postID。你可以据此显示数据。
    猜你喜欢
    • 2014-10-23
    • 1970-01-01
    • 1970-01-01
    • 2014-09-23
    • 2015-11-24
    • 1970-01-01
    • 1970-01-01
    • 2017-12-15
    • 1970-01-01
    相关资源
    最近更新 更多