如何在Codeigniter中使用联接查询从表中选择行?

时间:2018-07-18 07:07:16

标签: php codeigniter codeigniter-3 codeigniter-query-builder

我有两个表competition_registrationcompetition_schedule。我有一个用于从competition_registration中选择行的选择查询。这是我的代码,如下所示:

$this->db->select('competition_registration.permenent_registration_number');
    $this->db->from('competition_registration');
    $where = "permenent_registration_number is  NOT NULL";
    $this->db->where($where);
    $this->db->join('competition_schedule', 'competition_schedule.competition_schedule_id = competition_registration.competition_schedule_id');
    $this->db->join('competition_schedule', 'competition_schedule.period_id = 6');
    $this->db->join('competition_schedule', 'competition_schedule.competition_level_id = 3');
    $query = $this->db->get();  echo $this->db->last_query();exit;

但这显示一个错误。谁能检查这个查询并为我更正?

我想从两个表中选择competition_schedule_id表中的competition_level_idperiod_id等于3且competition_schedule等于6的列。

1 个答案:

答案 0 :(得分:1)

希望这对您有帮助:

competition_schedule.period_id = 6和这个competition_schedule.competition_level_id = 3放在where子句中,如下所示:

$this->db->select('competition_registration.permenent_registration_number');
$this->db->from('competition_registration');
$this->db->join('competition_schedule', 'competition_schedule.competition_schedule_id = competition_registration.competition_schedule_id');
$where = "competition_registration.permenent_registration_number is  NOT NULL";

$this->db->where($where);
$this->db->where('competition_schedule.period_id' , '6');
$this->db->where('competition_schedule.competition_level_id','3');

//$this->db->join('competition_schedule', 'competition_schedule.period_id = 6');
//$this->db->join('competition_schedule', 'competition_schedule.competition_level_id = 3');
$query = $this->db->get();  
echo $this->db->last_query();
exit;

更多信息:https://www.codeigniter.com/user_guide/database/query_builder.html