Php 代码点火器组_工作不正常

Php 代码点火器组_工作不正常,php,mysql,codeigniter,Php,Mysql,Codeigniter,我正在尝试从视频表中获取用户ID列表。此表包含用户上载的媒体文件。这是我的密码 $this->db->select("videos.user_id as userId"); $this->db->limit('10', $document['offset']); $this->db->group_by('userId'); $this->db->order_by('id','desc'

我正在尝试从视频表中获取用户ID列表。此表包含用户上载的媒体文件。这是我的密码

        $this->db->select("videos.user_id as userId");
        $this->db->limit('10', $document['offset']);
        $this->db->group_by('userId');
        $this->db->order_by('id','desc');

        $recentUploads = $this->db->get('videos')->result_array();
        if (!empty($recentUploads)) 
        {
            foreach ($recentUploads as $record) 
            {
                $totalPostMedia = $obj->totalPostMedia($record);
                $record['totalPostMedia'] = $totalPostMedia;

                $resData = $this->db->select("username, profileImage")->from('users')->where('id', $record['userId'])->get()->row_array();
                $record['uploadBy'] = $resData['username'];
                $record['profileImage'] = "http:...com/profileImage/".$resData['profileImage'];

                $Mydata[] = $record;
            }
        }
结果get缺少表中的一些userid。我也尝试过
$this->db->distinct()
。结果还是一样。获得无重复结果的唯一方法是删除
$this->db->order_by('id','desc')
或将其设置为
asc
而不是
desc
。但我想从表中获取最新记录。我该怎么做?我做错什么了吗?任何帮助都将不胜感激

试试这个

$this->db->select("videos.user_id as userId");
$this->db->from("videos");
$this->db->group_by('userId');
$this->db->order_by('id','desc');
$this->db->limit('10', $document['offset']);

$recentUploads = $this->db->get()->result_array();
if (count($recentUploads)>0) 
{
    foreach ($recentUploads as $record) 
    {
        $totalPostMedia = $obj->totalPostMedia($record);
        $record['totalPostMedia'] = $totalPostMedia;

        $resData = $this->db->select("username, profileImage")->from('users')->where('id', $record['userId'])->get()->row_array();
        $record['uploadBy'] = $resData['username'];
        $record['profileImage'] = "http:...com/profileImage/".$resData['profileImage'];

        $Mydata[] = $record;
    }
}
在您的请求(我的意思是选择videos.user\u id作为userId)中,您可以通过ligne将userId设置为完成组任务。您的用户ID别名不知道可以进行任何更改的列名称

为此,请用videos.user\u id替换组中的userId

您的代码将像这样为您工作

 $this->db->select("videos.user_id as userId");
    $this->db->limit('10', $document['offset']);
    $this->db->group_by('videos.user_id');
    $this->db->order_by('id','desc');

    $recentUploads = $this->db->get('videos')->result_array();
    if (!empty($recentUploads)) 
    {
        foreach ($recentUploads as $record) 
        {
            $totalPostMedia = $obj->totalPostMedia($record);
            $record['totalPostMedia'] = $totalPostMedia;

            $resData = $this->db->select("username, profileImage")->from('users')->where('id', $record['userId'])->get()->row_array();
            $record['uploadBy'] = $resData['username'];
            $record['profileImage'] = "http:...com/profileImage/".$resData['profileImage'];

            $Mydata[] = $record;
        }
    }
使用
max(id)

$this->db->选择(“videos.user\u id作为userId,
max(id)
”`

因此,您可以尝试:

    $this->db->select("videos.user_id as userId, max(id)");
    $this->db->limit('10', $document['offset']);
    $this->db->group_by('userId');
    $this->db->order_by('id','desc');

    $recentUploads = $this->db->get('videos')->result_array();
    if (!empty($recentUploads)) 
    {
        foreach ($recentUploads as $record) 
        {
            $totalPostMedia = $obj->totalPostMedia($record);
            $record['totalPostMedia'] = $totalPostMedia;

            $resData = $this->db->select("username, profileImage")->from('users')->where('id', $record['userId'])->get()->row_array();
            $record['uploadBy'] = $resData['username'];
            $record['profileImage'] = "http:...com/profileImage/".$resData['profileImage'];

            $Mydata[] = $record;
        }
    }

先写-->$this->db->order_by('id','desc');而不是$this->db->group_by('userId');试过了。仍然没有运气;你能把结果上传到这里让我看看吗