Php Codeigniter连接表以显示不同的内容结果
我有7个表来存储用户数据,比如帖子、图片、更新、评论、喜欢、回复和用户本身 我的问题是:如何使用正确的查询来执行联接表? 我正在使用此查询:Php Codeigniter连接表以显示不同的内容结果,php,mysql,codeigniter,Php,Mysql,Codeigniter,我有7个表来存储用户数据,比如帖子、图片、更新、评论、喜欢、回复和用户本身 我的问题是:如何使用正确的查询来执行联接表? 我正在使用此查询: if ( ! function_exists('getTimeline')) { function getTimelines($contributor = null, $limit = 10, $offset = 0) { $CI =& get_instance(); $CI->db->
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)都具有相同的值。查询正在生成部分叉积 对于从
\u用户
返回的每一行,MySQL将从\u用户
获取所有匹配行
举个例子,我们假设有一行是从\u用户
返回的,在\u likes
中有四个匹配的行,返回(到目前为止)总共四行。来自\u users
的行与来自\u likes
的四行中的每一行匹配。来自\u users
的行中的所有列都复制到四行中的每一行中
从\u posts
表中,为了举例,我们假设有两行匹配。因此,从\u posts
返回的这两行中的每一行都将与我们已经拥有的四行中的每一行相匹配,总共有八行。(从\u posts
返回的每一行都与从\u likes
返回的每一行相匹配)
从\u comments
表中,例如,假设返回了六行。这些行中的每一行都与我们已有的八行匹配,总共有48行。每个表的列中的很多值都被“复制”到新行中,因为新表中的多个行被加入
依此类推,每增加一个联接表
这是表的部分“叉积”。(半笛卡尔积?)
如果要返回不同的
\u帖子列表
、不同的\u喜欢列表
、不同的\u评论列表
,等等,则可以对每个表运行单独的查询。这将避免由于连接操作而发生的“重复”。这可能是最简单的方法
否则,如果您想从当前查询返回的结果集中获得一个独特的\u帖子
,\u喜欢
,\u评论
,等等的列表,则需要客户端筛选行,以过滤出重复的\u帖子
,\u评论
。对于返回的行中包含的每个表,您需要有一个唯一的标识符
基本上,您的代码需要为
\u posts
、\u like
、\u comments
构建单独的数组。对于结果集中的每一行,您需要检查\u posts
列中的值是否来自您已经处理的\u posts
中的一行。如果它是您已经处理过的,则丢弃它,否则,将其添加到数组中。从本质上讲,将行从每个表中分解为单独的结果,其形式与从每个表的单独查询中得到的结果相同。谢谢,我知道了。你有任何关于建造墙壁系统的线索吗?如何获取与用户ID匹配的所有结果,并将其循环到一个foreach结果中,然后分离出不同类型的表?