简体   繁体   English

Codeigniter不同的mysql查询

[英]codeigniter distinct mysql query

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. 每当我在模型中使用get_where时,它就会为我提供所有数据库条目(包括重复项),而当我使用get时,它只会为我提供第一个条目。 But I want all distinct entries. 但是我想要所有不同的条目。 Can anyone help me please. 任何人都可以帮助我。 Thanks a lot! 非常感谢!

Controler = site.php: 控制器= 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 型号= 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 视图= content_western_australia.php

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

Distinct will not always work. 区别并不总是有效。 You should add - >group_by("name_of_the_column_which_needs_to_be unique"); 您应该添加- >group_by("name_of_the_column_which_needs_to_be unique");

From the CI docs: CI文档:

 $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: 为了使事情更简单,Active Record允许您编写查询:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM