1

我正在尝试使用 CodeIgniter activercord 类进行连接查询,如下所示:

$query = $this->db->select('accredited_majors, all_majors_accredited, accredited')
                    ->select('universities.name, universities.slug')
                    ->from('accreditations')
                    ->join('universities', 'universities.id = '.$uni_id)
                    ->where('accreditations.university_id', $uni_id)
                    ->where('accreditations.country', $country)
                    ->get();

但我收到这个错误:

A Database Error Occurred
Error Number: 1054

Unknown column '820' in 'on clause'

SELECT `accredited_majors`, `all_majors_accredited`, `accredited`, `default_universities`.`name`, `default_universities`.`slug` FROM (`default_accreditations`) JOIN `default_universities` ON `default_universities`.`id` = `820` WHERE `default_accreditations`.`university_id` = '820' AND `default_accreditations`.`country` = 'AE'

我相信错误出现在 join() 行中:

->join('universities', 'universities.id = '.$uni_id)

如何在 join() 函数中包含变量?

4

3 回答 3

2

在您的代码中,您有 2 个 WHERE 子句而没有ON,并且您的连接在 on 子句中是错误的。

尝试这个

    $query = $this->db->select('accredited_majors, all_majors_accredited, accredited')
                ->select('universities.name, universities.slug')
                ->from('accreditations')
                ->join('universities', 'universities.id = accreditations.university_id')
                ->where('accreditations.university_id', $uni_id)
                ->where('accreditations.country', $country)
                ->get();
于 2013-01-09T08:46:18.870 回答
0

尝试这个

$query = $this->db->select('accredited_majors, all_majors_accredited, accredited')
                    ->select('universities.name, universities.slug')
                    ->from('accreditations')
                    ->join('universities', "universities.id = accreditations.$uni_id" , 'left')
                    ->where('accreditations.university_id', $uni_id)
                    ->where('accreditations.country', $country)
                    ->get();
于 2013-01-09T08:54:47.910 回答
0

尝试这个

$this->db->select('accredited_majors, all_majors_accredited, accredited')
->select('universities.name, universities.slug')
->from('accreditations')
->join('universities', 'universities.id = accreditations.university_id', 'left')
->where('accreditations.university_id', $uni_id)
->where('accreditations.country', $country)
->group_by('accreditations.university_id')
->get();
于 2013-01-09T09:24:13.480 回答