笨不同的MySQL查询(codeigniter distinct mysql query)

2019-09-21 17:14发布

每当我在模型中使用get_where它给了我所有数据库条目,包括复制,当我用得到的只有它只是给我的第一个条目。 但我想所有不同的条目。 谁能帮我请。 非常感谢!

CONTROLER = site.php:

public function western_australia(){
    $this->load->model("continents_get");
    $data["results"] = $this->continents_get
                            ->getMaincategories("western_australia");
    $this->load->view("content_western_australia", $data);
}

模型= continents_get.php

function getMaincategories($western_australia){
    $this->db->distinct();
    $query = $this->db->get_where("p_maincategories", 
                    array("page" => $western_australia));
    return $query->result();
}

视图= content_western_australia.php

<?php
    foreach($results as $row){
        $page = $row->page;
        $main_en = $row->main_en;
        echo $main_en; 
?>

Answer 1:

不同的并不总是可行的。 您应该添加- >group_by("name_of_the_column_which_needs_to_be unique");



Answer 2:

从CI文档:

 $this->db->distinct();

Adds the "DISTINCT" keyword to a query

$this->db->distinct();
$this->db->get('table');

// Produces: SELECT DISTINCT * FROM table

也许就是为什么你只有一条线。

为了让事情更简单的Active Record让你写你的查询:

$query = $this->db->query('SELECT DISTINCT main_en FROM p_maincategories 
WHERE PAGE =  '.$this->db->escape($western_australia).');


文章来源: codeigniter distinct mysql query