【问题标题】:Order by and group by not working in php codeigniter?通过不在 php codeigniter 中工作的排序和分组?
【发布时间】:2017-03-16 16:35:11
【问题描述】:

表格格式: 表名类别:

      id       title
   -----------------
      1          xyz
      2          abc

表名子类:

     id        category_id    start_date      status
 ----------------------------------------------------------
     1             1            2016-11-03       1
     2             1            2016-11-09       1
     3             2            2016-10-03       1
     4             3            2016-12-03       1
     5             3            2016-12-05       1
     6             5            2016-12-06       1

查询:

      $date = date('Y-m-d');
      $this->db->select('*');
      $this->db->where('status',1);
      $this->db->where('start_date >=',$date);
      $this->db->order_by('start_date', 'asc');
      $this->db->group_by('category_id');
      $this->db->limit(4);
      $query = $this->db->get('subcategory');
      return $query->result();

我的问题是:我希望结果为 subcategory_id 1,4,6 。但是 group_by 和 order by 不起作用。

错误:

SELECT 列表不在 GROUP BY 子句中并且包含非聚合 列“dbname.subcategory.id”在功能上不依赖于 GROUP BY 子句中的列;这与 sql_mode=only_full_group_by SELECT * FROM (subcategory) WHERE status = 1 AND start_date >= '2016-11-03' GROUP BY category_id ORDER BY start_date asc LIMIT 4

【问题讨论】:

  • 错误信息是什么??
  • SELECT 列表不在 GROUP BY 子句中,并且包含非聚合列“dbname.subcategory.id”,它在功能上不依赖于 GROUP BY 子句中的列;这与 sql_mode=only_full_group_by SELECT * FROM (subcategory) WHERE status = 1 AND start_date >= '2016-11-03' GROUP BY category_id ORDER BY start_date asc LIMIT 4不兼容>
  • 你的mysql版本是7吗?
  • 将此行添加到您的 my.cnf 文件 sql_mode = "STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION" 的末尾。然后重启mysql。

标签: php sql codeigniter


【解决方案1】:
$date = date('Y-m-d');
$this->db->select('*');
$this->db->group_by('category_id');
$this->db->limit(4);
$query = $this->db->get("(SELECT * FROM subcategory where status=1 and start_date>='$date' order by start_date) as subcategory");
return $query->result();

【讨论】:

  • 用户想要所有数据,而不仅仅是逗号分隔的 id。另外,添加一些应该更改的信息。
  • 你能解释一下吗?
【解决方案2】:

更改您的查询: 首先应该有 group by 然后 order by

$date = date('Y-m-d');
$this->db->select('*');
$this->db->where('status',1);
$this->db->where('start_date >=',$date);
$this->db->group_by('category_id');
$this->db->order_by('start_date', 'asc');
$this->db->limit(4);
$query = $this->db->get('subcategory');
return $query->result();

或者通过在$query 之后执行此操作来打印查询并在数据库中检查相同的内容:

echo $this->db->last_query();

【讨论】:

  • 我认为在使用 ActiveRecord 时将 group_by 放在哪里并不重要。您可以通过打印查询来验证它。
  • @SayantanDas 让他试试。
  • 我尝试了您的代码,但它给出了相同的错误 SELECT list is not in GROUP BY 子句并包含非聚合列 'dbname.subcategory.id',它在功能上不依赖于 GROUP BY 子句中的列;这与 sql_mode=only_full_group_by SELECT * FROM (subcategory) WHERE status = 1 AND start_date >= '2016-11-03' GROUP BY category_id ORDER BY start_date asc LIMIT 4 不兼容
  • @K.doe 这意味着您在使用 group by 子句时必须使用一个聚合函数。
猜你喜欢
  • 1970-01-01
  • 2016-03-20
  • 1970-01-01
  • 1970-01-01
  • 2018-06-19
  • 2013-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多