我有7个表来存储用户数据,比如帖子、图片、更新、评论、喜欢、转发和用户本身。
php - Codeigniter连接表以显示不同的内容结果-LMLPHP
这里是我的问题:如何使用右查询来执行连接表?
我使用以下查询:

if ( ! function_exists('getTimeline'))
{
    function getTimelines($contributor = null, $limit = 10, $offset = 0)
    {
        $CI =& get_instance();
        $CI->db->select('
            abycms_posts.*,
            abycms_images.imageID,
            abycms_images.contributor as owner,
            abycms_images.imageContent,
            abycms_images.imageFile,
            abycms_images.imageSlug,
            abycms_images.timestamp as date,
            abycms_images.visits_count as visits,
            abycms_updates.updateID,
            abycms_updates.userID as updater,
            abycms_updates.updateContent,
            abycms_updates.visibility,
            abycms_updates.timestamp as update_time,
            abycms_likes.likeID,
            abycms_likes.userID as userLike,
            abycms_likes.type as likeType,
            abycms_likes.timestamp as like_time,
            abycms_comments.commentID,
            abycms_comments.userID as commentUser,
            abycms_comments.type as commentType,
            abycms_comments.timestamp as comment_time,
            abycms_reposts.repostID,
            abycms_reposts.userID as repostUser,
            abycms_reposts.type as repostType,
            abycms_reposts.timestamp as repost_time
        ');
        $CI->db->from('abycms_users');
        $CI->db->join('abycms_posts', 'abycms_posts.contributor = abycms_users.userID', 'left');
        $CI->db->join('abycms_images', 'abycms_images.contributor = abycms_users.userID', 'left');
        $CI->db->join('abycms_updates', 'abycms_updates.userID = abycms_users.userID', 'left');
        $CI->db->join('abycms_likes', 'abycms_likes.userID = abycms_users.userID', 'left');
        $CI->db->join('abycms_comments', 'abycms_comments.userID = abycms_users.userID', 'left');
        $CI->db->join('abycms_reposts', 'abycms_reposts.userID = abycms_users.userID', 'left');
        $CI->db->where('abycms_users.userID', $contributor);
        $CI->db->limit($limit, $offset);

        // How to order results by newest `timestamp` for posts, images, updates, comments, likes or reposts?
        $CI->db->order_by('abycms_posts.timestamp', 'desc');

        // How to handle not duplicate `postID` or `imageID` also group it by different `type`s?
        $CI->db->group_by('abycms_posts.postID, abycms_images.imageID');

        $query = $CI->db->get();

        if($query->num_rows() > 0)
        {
            return $query->result_array();
        }
        else
        {
            return array();
        }
    }
}

我的视图处理不同类型的结果:
foreach(getTimelines($page['userID'], $limit, $offset) as $row)
{
    if($row['updateID'] != null) // Updating Status
    {
        // This status updates
    }
    elseif($row['postID'] != null) // Writing Article
    {
        // This is posts
    }
    elseif($row['imageID'] != null) // Uploading Image
    {
        // This is images
    }
    elseif($row['commentID'] != null) // Commented on Post
    {
        // This is comments
    }
    elseif($row['likeID'] != null) // Liking User Post
    {
        // This is likes
    }
    elseif($row['repostID'] != null) // Reposting User Post
    {
        // This is reposts
    }
}

当我使用上面的查询时,结果会显示出来,但我不知道要分离内容类型。它始终显示为状态更新,并且所有唯一的id(如postID、imageID、updateID、repostID、likeID和commentID)都具有相同的值。

最佳答案

查询正在生成部分叉积。
对于从_users返回的每一行,MySQL将从_likes获取所有匹配的行。
为了一个例子,我们假设有一行从_users返回,_likes中有四个匹配的行,总共返回了四行。_users中的行与_likes中的四行中的每一行匹配。_users行中的所有列都复制到四行中的每一行中。
_posts表中,为了一个例子,我们假设有两行匹配。因此,从_posts返回的这两行中的每一行都将与我们已经拥有的四行中的每一行匹配,总共得到八行。(从_posts返回的每一行与从_likes返回的每一行匹配)
在这个例子中,假设从_comments表返回了六行。这些行中的每一行都与我们已经拥有的八行匹配,总共有48行。当新表中的多个行被合并时,每个表的列中的许多值将被“复制”到新行中。
依此类推,每增加一个连接表。
这是表的部分“交叉积”。(半笛卡尔积?)
如果要返回不同的_posts列表、不同的_likes列表和不同的_comments列表等,则可以对每个表运行单独的查询。这样可以避免由于连接操作而发生的“重复”。这可能是最简单的方法。
否则,如果要获得_posts_likes_comments等的不同列表。在当前查询返回的结果集中,您需要客户机筛选行以筛选出重复的_posts_likes_comments。对于返回行中包含的每个表,您需要有一个唯一的标识符。
基本上,您的代码需要为_posts_likes_comments构建单独的数组。对于结果集的每一行,您需要检查_posts列中的值是否来自您已经处理过的_posts行。如果是已经处理过的,则丢弃它,否则,将其添加到数组中。基本上是将行从每个表中消除为单独的结果,其格式是从每个表的单独查询中获得的。

10-01 08:23
查看更多