[英]codeigniter inner join tables syntax
錯誤消息:“ where子句”中的未知列“ category”
我必須內部聯接兩個表。 正確的查詢應該是什么? 或者我應該在$ query-> get()中輸入什么參數? 如果僅放“電影”,則無法在另一個表中找到“類別”列。
$query = $this->db->select('title, name as category, rental_rate, length')->order_by($sort_by, $sort_order);
$query = $this->db->join('film_category', 'film_category.film_id = film.film_id');
$query = $this->db->join('category', 'film_category.category_id = category.category_id');
if(strlen($query_array['title'])) {
$query->like('title', $query_array['title']);
}
if(strlen($query_array['category'])) {
$query->where('category', $query_array['category']);
}
$data['films'] = $query->get('film', 20, $this->uri->segment(6));
$this->db->select('title, name as category, rental_rate, length')->order_by($sort_by, $sort_order);
$this->db->from('film'); /*I assume that film was the table name*/
$this->db->join('film_category', 'film_category.film_id = film.film_id');
$this->db->join('category', 'category.category_id = film_category.category_id');
$query = $this->db->get();
var_dump($query);
仔細檢查我添加的代碼,並確保在類別表上,該列名為category_id,而不僅僅是id,並且在film_category下,有一個category_id列。 如果使用我提交的代碼,您仍然收到錯誤,請嘗試將第一行替換為
$this->db->select('title, name, rental_rate, length')->order_by($sort_by, $sort_order);
我不確定使用與表匹配的名稱是否會對CodeIgniter和ActiveRecord造成麻煩。
希望能有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.