2014-11-23 47 views
0

我有一个分类表来命名每个类别,然后我有一个艺术品表格,它有三列,每个艺术片段提供三个不同的类别。我正在构建一个select查询,它将查看一个类别的所有三列,并加入类别表,以便获取类别名称。我有每个连接的别名,但我的查询仍然返回category_id2和category_id3的空值。任何援助非常感谢。Codeigniter - MySql:使用另一个表的ID加入一个表的三个列值

从笨型号:

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

您不需要选择所有tabels.just使用'$ this - > db - > from('ap_mini_artist a')'并使用'$ this - > db - > where('c.category',$ category);' – 2014-11-23 19:57:08

+0

Thanks @ShaifulIslam for your comment。当我尝试将一个表中的三列连接到另一个表的ID时,我仍然收到空值。有什么建议么?谢谢 – Jay 2014-11-23 20:52:12

+0

尝试内部联接,类似的问题在这里:http://stackoverflow.com/a/27053108/2009536 – avenda 2014-11-23 21:42:02

回答

1

您不需要与类别表中的三个连接。

关于您的活动记录查询::不是所有三个领域公认的比赛加入

  • 当你加入,你不需要from子句中重复的表名
  • where函数没有after参数,将其更改为db->like(c.category', $category, 'after'
+0

工程很好。谢谢阿米尔! – Jay 2014-11-26 00:57:48

相关问题