【问题标题】:Unknown column '' in 'on clause' in CodeIgniterCodeIgniter 中“on 子句”中的未知列“”
【发布时间】:2012-12-23 06:15:34
【问题描述】:

我正在尝试使用 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() 函数中包含变量?

【问题讨论】:

  • 我觉得应该是join('universities', 'universities.id = accreditations.university_id')

标签: mysql codeigniter activerecord join


【解决方案1】:

在您的代码中,您有 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();

【讨论】:

  • codeigniter activerecord 中是否有一个名为 ON() 的函数?这给了我这个错误:Fatal error: Call to undefined method CI_DB_mysql_driver::on()
【解决方案2】:

试试这个

$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();

【讨论】:

    【解决方案3】:

    试试这个

    $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();
    

    【讨论】:

      猜你喜欢
      • 2011-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-26
      • 2020-04-25
      相关资源
      最近更新 更多