codeigniter内部连接不起作用

时间:2015-12-07 06:03:08

标签: php mysql codeigniter

我想用codeigniter做一个内部联接,我已经尝试了很多但没有成功。

内连接的Mysql代码,

SELECT * FROM shop INNER JOIN city ON city.city_id = shop.city_id WHERE city.city_name = 'Bangalore'

上面的SQL查询在phpmyadmin中运行得很好。在codeigniter中转换此代码时,它无效。

Codeigniter代码是,

$this->db->select('*');
$this->db->from('shop');
$this->db->join('city', 'city.city_id = shop.city_id');
$query = $this->db->where('city', array('city.city_name' => 'Bangalore'));

if($query->num_rows() > 0) {
    return $result = $query->result_array();
}
else {
    return 0;
}

哪里出错了。我是codeigniter的初学者。

3 个答案:

答案 0 :(得分:2)

您的查询应运行良好:

$this->db->select('*');
$this->db->from('shop');
$this->db->join('city', 'city.city_id = shop.city_id');
//$this->db->where('city', array('city.city_name' => 'Bangalore'));
$this->db->where('city.city_name', 'Bangalore');
$query = $this->db->get();

答案 1 :(得分:1)

试试这个:

$query = $this->db->select('*')
          ->from('shop')
          ->join('city', 'city.city_id = shop.city_id', 'inner')
          ->where('city.city_name', 'Bangalore');
          ->get();

答案 2 :(得分:1)

您错过了来自db的<{1}}数据,

将此更改为

get()

$query = $this->db->where('city', array('city.city_name' => 'Bangalore'));

您无需$this->db->where('city', array('city.city_name' => 'Bangalore')); $query = $this->db->get();// add this line. 即可使用from()并将表名传递给它。

get()