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

これを試して

$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 に答える
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 に答える