我有一个用于命名每个类别的类别表,然后我有一个有三列的艺术作品表,每个艺术作品提供三种不同的类别。我正在构建一个选择查询,它将查看一个类别的所有三列并加入类别表,以便我可以获取类别名称。我有别名'对于每个连接,但我的查询仍然返回category_id2和category_id3的空值。非常感谢任何帮助。
来自Codeigniter模型:
function category_search($category) {
$this -> db -> select('a.id, a.artist_fname, a.artist_lname, b.artist_id, b.sm_file_name, b.category_id, b.category_id2, b.category_id3, c.id, c.category');
$this -> db -> from('ap_mini_artist a', 'ap_mini_artwork b', 'ap_art_categories c', 'ap_art_categories c2', 'ap_art_categories c3');
$this -> db -> where('c.category', $category, 'after');
$this -> db -> join('ap_mini_artwork b', 'b.artist_id=a.id', 'left');
$this -> db -> join('ap_art_categories c', 'c.id=b.category_id', 'left');
$this -> db -> join('ap_art_categories c2', 'c2.id=b.category_id2', 'left');
$this -> db -> join('ap_art_categories c3', 'c3.id=b.category_id3', 'left');
$query = $this -> db -> get();
return $query -> result();
}
如果需要,从控制器:
public function category_search() {
$category = $this -> input -> post('categoryValue');
$query = $this -> mini_show_model -> category_search($category);
if (!empty($query)) {
$json = json_encode($query);
print $json;
} else {
echo "Your query is empty, please try again.";
}
}
感谢您的帮助!
答案 0 :(得分:1)
您不需要与类别表进行三次连接。而是将所有三个字段作为可接受的匹配加入:
select a.id, a.artist_fname, a.artist_lname, b.artist_id, b.sm_file_name, b.category_id, b.category_id2, b.category_id3, c.id, c.category
from ap_mini_artist a
left join ap_mini_artwork b on b.artist_id = a.id
left join ap_art_categories c on c.id in (b.category_id, b.category_id2, b.category_id3)
where c.category LIKE ?
关于您的有效记录查询:
from
子句where
函数没有after
个参数,请将其更改为db->like(c.category', $category, 'after'