[英]codeigniter inner join not working
我想与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的初学者。
您的查询应该运作良好:
$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();
尝试这个:
$query = $this->db->select('*')
->from('shop')
->join('city', 'city.city_id = shop.city_id', 'inner')
->where('city.city_name', 'Bangalore');
->get();
您错过了从db 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()
并将表名传递给它,就像这样。
$this->db->select('*');
//$this->db->from('shop'); <-- no need of this line
$this->db->join('city', 'city.city_id = shop.city_id');
$this->db->where('city', array('city.city_name' => 'Bangalore'));
$query = $this->db->get("shop");//<-- give table name here only.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.