2015-12-31 48 views
2

我有7个表来存储用户的数据,帖子,图片,更新,评论,喜欢,转播和用户本身。笨连接表显示不同的内容,结果

enter image description here

这里是我的问题:如何使用权的查询执行联接表? 我使用这个查询:

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 
    } 
} 

当我使用上面的查询,结果被显示出来,但我不想法分开内容类型。它始终显示为状态更新,并且所有唯一标识如postID,imageID,updateID,repostID,likeID和commentID具有相同的值。

回答

1

该查询生成的局部横产物。

对于从_users返回的每一行,MySQL都会从_likes获取所有匹配行。

为了举例,我们假设有一行从_users返回,并且在_likes中有四个匹配行,返回(到目前为止)总共四行。从_users行会从_likes匹配四个行。来自_users行的所有列都被复制到四行中的每一行中。

而从_posts表中,为了举例说明,我们假设有两个行匹配。因此,从_posts返回的那两行中的每一行都将与我们已有的四行中的每一行匹配,总共给出八行。 (从_posts返回的每一行都与从_likes返回的每一行匹配。)

_comments表中,对于此示例,假设有六行返回。这些行中的每一行都与我们已有的八行相匹配,总共给出了48行。和很多从每个表的列值的越来越“复制”到新行,来自新表中的多个行结合。

依此类推,每增加一个连接表。

这是一个局部的“跨界产品”的表。 (A半笛卡尔乘积?)


如果你想返回_posts不同的清单,_likes鲜明的列表,以及_comments不同的列表,等等,那么你可以运行单独的查询每个表。这将避免由于联接操作而发生的“重复”。这可能是最简单的方法。否则,如果你想得到一个清晰的列表_posts_likes_comments,et al。走出目前的查询返回结果集的,你需要在客户端通过行筛选,过滤掉重复_posts_likes_comments。您需要为返回的行中包含的每个表具有唯一标识符。

基本上,您的代码需要为_posts,_likes_comments构建单独的阵列。对于结果集中的每一行,您需要检查_posts列中的值是否来自您已经处理的_posts行。如果这是你已经处理过的一个,放弃它,否则,将它添加到数组中。基本上是将行从每个表中分离出来,并以单独的查询形式从每个表中取出。

+0

谢谢,我明白了。 您是否有任何建筑墙体系统的线索?如何获取与用户ID匹配的所有结果并将其在一个foreach结果中循环,然后分离表的类型? –

相关问题