【问题标题】:mySQL help in codeignitercodeigniter 中的 mySQL 帮助
【发布时间】:2011-01-15 16:10:29
【问题描述】:

我正在运行查询以填充下拉菜单,但是我使用公司名称列表的列,这意味着公司名称经常重复,有没有办法只获取每个重复值一次?因此,例如,如果我在表格中有类似的东西,

Company 1
Company 1
Company 2
Company 3
Company 4
Company 4

但我希望下拉菜单返回,

Company 1
Company 2
Company 3
Company 4

我正在使用数据库库和活动记录来编写这样的sql,目前我需要知道我使用什么来只显示每个结果一次,

function getAllUsers() {
    $this->db->select('*');
    $this->db->from('userProfileTable');

    $query = $this->db->get();
    return $query->result_array();
}

或者原始的 sql 是什么?

【问题讨论】:

    标签: mysql database codeigniter


    【解决方案1】:

    用途:

    $this->db->distinct();
    $this->db->select('*');
    $this->db->from('userProfileTable');
    $query = $this->db->get();
    return $query->result_array();
    

    【讨论】:

      【解决方案2】:

      您可以使用$this->db->distinct();DISTINCT 关键字添加到您的查询中。

      将你的函数修改为:

      function getAllUsers() {
          $this->db->distinct();
          $query = $this->db->get('userProfileTable');
      
          return $query->result_array()
      }
      

      产生

      SELECT DISTINCT * FROM userProfileTable
      

      请注意,selectfrom 已从您的原始函数中删除,因为 get 只是做同样的事情。

      【讨论】:

        【解决方案3】:

        使用 DISTINCT 关键字:

        SELECT DISTINCT * from userProfileTable;
        

        【讨论】:

        • 您是否阅读过这部分问题:“或者原始 sql 会是什么?” ?
        【解决方案4】:

        这应该可以解决问题:

        function getAllUsers() {
            $this->db->distinct();
            $query = $this->db->get('userProfileTable');
            return $query->result_array();
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-10-23
          • 2011-01-04
          • 1970-01-01
          • 1970-01-01
          • 2011-02-23
          相关资源
          最近更新 更多