获取连接表不能正常工作

时间:2015-04-21 09:18:55

标签: mysql codeigniter join

当我使用$this->db->where()& $this->db->join()它没有检索任何数据。 var dump表示 NULL

在我的旧代码上,它在查询中使用JOIN正常工作。无法解决我所缺少的问题。

旧代码正常

public function get_admin_category($category_id) {
$language_id = $this->check_language();

$query = $this->db->query("SELECT DISTINCT *, (SELECT GROUP_CONCAT(cd1.name ORDER BY level SEPARATOR '  >  ') FROM " . $this->db->dbprefix . "category_path cp LEFT JOIN " . $this->db->dbprefix . "category_description cd1 ON (cp.path_id = cd1.category_id AND cp.category_id != cp.path_id) WHERE cp.category_id = c.category_id AND cd1.language_id = '" . (int)$language_id . "' GROUP BY cp.category_id) AS path, (SELECT DISTINCT keyword FROM " . $this->db->dbprefix . "url_alias WHERE query = 'category_id=" . (int)$category_id . "') AS keyword FROM " . $this->db->dbprefix . "category c LEFT JOIN " . $this->db->dbprefix . "category_description cd2 ON (c.category_id = cd2.category_id) WHERE c.category_id = '" . (int)$category_id . "' AND cd2.language_id = '" . (int)$language_id . "'");

return $query->row_array();
}

新代码我的所作所为。

public function get_admin_category($category_id) {
    $language_id = $this->check_language();
    $this->db->select('GROUP_CONCAT(cd1.name ORDER BY level SEPARATOR "  >  ")');
    $this->db->distinct();
    $this->db->from($this->db->dbprefix . 'category_path cp', 'LEFT');
    $this->db->join($this->db->dbprefix . 'category_description cd1', 'cp.path_id = cd1.category_id AND cp.category_id != cp.path_id', 'LEFT');
    $this->db->where('cp.category_id', 'c.category_id');
    $this->db->where('cd1.language_id', $language_id);
    $query = $this->db->get();
    return $query->row_array();
}

如何将旧的$ this-> db->查询转换为使用连接的codeigniter工作。 http://www.codeigniter.com/userguide2/database/active_record.html

2 个答案:

答案 0 :(得分:1)

试试这个,它会为你工作。

$this->db->select("DISTINCT *,(SELECT GROUP_CONCAT(cd1.name ORDER BY level SEPARATOR '  >  ') FROM " . $this->db->dbprefix . "category_path cp LEFT JOIN " . $this->db->dbprefix . "category_description cd1 ON (cp.path_id = cd1.category_id AND cp.category_id != cp.path_id) WHERE cp.category_id = c.category_id AND cd1.language_id = '" . (int)$language_id . "' GROUP BY cp.category_id) AS path, (SELECT DISTINCT keyword FROM " . $this->db->dbprefix . "url_alias WHERE query = 'category_id=" . (int)$category_id . "') AS keyword", false);
$this->db->from($this->db->dbprefix . "category c");
$this->db->join($this->db->dbprefix . "category_description cd2", "c.category_id = cd2.category_id", "left");
$this->db->where("c.category_id", intval($category_id));
$this->db->where("cd2.language_id", intval($language_id));
$result = $this->db->get()->row_array();
return $result;

答案 1 :(得分:0)

试试这个:

$this->db->select('DISTINCT *');
$this->db->select("(SELECT GROUP_CONCAT(cd1.name ORDER BY level SEPARATOR '  >  ') FROM " . $this->db->dbprefix . "category_path cp LEFT JOIN " . $this->db->dbprefix . "category_description cd1 ON (cp.path_id = cd1.category_id AND cp.category_id != cp.path_id) WHERE cp.category_id = c.category_id AND cd1.language_id = '" . (int)$language_id . "' GROUP BY cp.category_id) AS path", FALSE);
$this->db->select("(SELECT DISTINCT keyword FROM " . $this->db->dbprefix . "url_alias WHERE query = 'category_id=" . (int)$category_id . "') AS keyword", FALSE);
$this->db->from($this->db->dbprefix . "category c");
$this->db->join($this->db->dbprefix . "category_description cd2", "c.category_id = cd2.category_id", "left");
$this->db->where("c.category_id", intval($category_id));
$this->db->where("cd2.language_id", intval($language_id));