问题
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