以下查询将输出显示为
Array ( [0] => stdClass Object ( [comment_ID] => 8 [ID] => 41 ) [1] => stdClass Object ( [comment_ID] => 12 [ID] => 35 ) [2] => stdClass Object ( [comment_ID] => 7 [ID] => 35 ) [3] => stdClass Object ( [comment_ID] => 5 [ID] => 35 ) )
我需要显示一个[ID},其第一个[comment_ID]不需要2和第3个数据数组...而不是重复[ID]及其[comment_ID]我需要另一个[ID]的数据
function latest_post() {
$this->db->select('comment_ID, ID');
$this->db->from('user_posts');
$this->db->join('user_comments', 'comment_post_ID = ID');
$this->db->where('post_status', 1);
$this->db->where('comment_approved', 1);
$this->db->where('comment_rate', 3);
$this->db->order_by("ID", "desc");
$this->db->limit(4);
$query = $this->db->get();
return $query->result();
}
答案 0 :(得分:0)
使用group_by删除结果中的ID重复
$this->db->select('comment_ID, ID');
$this->db->from('user_posts');
$this->db->join('user_comments', 'comment_post_ID = ID');
$this->db->where('post_status', 1);
$this->db->where('comment_approved', 1);
$this->db->where('comment_rate', 3);
$this->db->group_by("ID");
$this->db->order_by("ID", "desc");
$this->db->limit(4);
$query = $this->db->get();
return $query->result();
这将导致每个ID的单个记录
答案 1 :(得分:0)
使用连接时,请尝试始终在选择中定义表。
$query = $this->db->select('user_posts.comment_ID, user_posts.ID')
->from('user_posts')
->join('user_comments', 'user_comments.comment_post_ID = user_posts.ID')
->where('post_status', 1)
->where('comment_approved', 1)
->where('comment_rate', 3)
->order_by("ID", "desc")
->limit(4)
->get();
return $query->result();
另请注意,我删除了重复的$ this-> db