[英]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.