codeigniter distinct mysql query

我是研究僧i 提交于 2019-12-23 01:13:22

问题


whenever I use get_where in the model it gives me all database entries including duplications and when I use get only it only gives me the first entry. But I want all distinct entries. Can anyone help me please. Thanks a lot!

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);
}

model = continents_get.php

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

view = content_western_australia.php

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

回答1:


Distinct will not always work. You should add ->group_by("name_of_the_column_which_needs_to_be unique");




回答2:


From the CI docs:

 $this->db->distinct();

Adds the "DISTINCT" keyword to a query

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

// Produces: SELECT DISTINCT * FROM table

Probably why you get only one line.

To keep things more simple Active Record allows you to write your query:

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


来源:https://stackoverflow.com/questions/12130905/codeigniter-distinct-mysql-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!