我很难将MySQL查询转换为Codeigniter查询生成器。我无法使用同一张表进行内部联接。就像我的mySQL查询一样。它正在使用mySQL。这是我的查询:

$sql = "SELECT answers.* FROM answers INNER JOIN
                (SELECT * FROM
                    (SELECT employee_id as test_employee_id, created_on as test_created_on FROM answers) as test
                GROUP BY test.test_employee_id)
                AS test on (test.test_employee_id = answers.employee_id AND test.test_created_on = answers.created_on)
                ORDER BY answers.created_on DESC";


你们能帮我吗?我正在尝试将其转换为Codeigniter查询。

最佳答案

试试这个:

$this->db->select('answers.*');
$this->db->from('answers');
$this->db->join('(SELECT * FROM
                    (SELECT employee_id as test_employee_id, created_on as test_created_on FROM answers) as test1
                GROUP BY test1.test_employee_id)
                AS test', 'test.test_employee_id = answers.employee_id AND test.test_created_on = answers.created_on');
$this->db->order_by("answers.created_on", "desc");
$query = $this->db->get();
return $query->result();

关于php - 具有内部联接的Codeigniter子查询,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59807687/

10-12 12:46