Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在CodeIgniter中查询大多数购买的数据_Php_Sql_Codeigniter - Fatal编程技术网

Php 在CodeIgniter中查询大多数购买的数据

Php 在CodeIgniter中查询大多数购买的数据,php,sql,codeigniter,Php,Sql,Codeigniter,事务表 标题: 1PHP食谱 2C++初学者BR/> 3高级Java 4PHP食谱 5PHP食谱 6高级Java 我想创建一个从交易表中购买最多图书的列表,如上图所示,并生成如下列表: $this->db->select('Title'); $this->db->from('transaction'); $this->db->count('Title AS TitleCount'); $this->db->group_by('AssetTitle'

事务表
标题:
1PHP食谱
2C++初学者BR/> 3高级Java
4PHP食谱
5PHP食谱
6高级Java

我想创建一个从交易表中购买最多图书的列表,如上图所示,并生成如下列表:

$this->db->select('Title');
$this->db->from('transaction');
$this->db->count('Title AS TitleCount');
$this->db->group_by('AssetTitle');
$this->db->order_by('TitleCount','ASC');
$this->db->limit(4);
$temp = $this->db->get();
$temp->num_rows();
return $temp->result_array();
购买最多的:

  • PHP食谱
  • 高级Java
  • C++初学者
  • 我使用的是PHP CI,我所做的如下:

    $this->db->select('Title');
    $this->db->from('transaction');
    $this->db->count('Title AS TitleCount');
    $this->db->group_by('AssetTitle');
    $this->db->order_by('TitleCount','ASC');
    $this->db->limit(4);
    $temp = $this->db->get();
    $temp->num_rows();
    return $temp->result_array();
    
    但结果不是我想要的。代码点火器无法识别计数。
    我不太了解这个查询,所以如果有人能帮我处理这个查询,我将不胜感激

    $this->db->select('Title,count(Title) AS TitleCount',FALSE);
    $this->db->from('transaction');
    $this->db->group_by('AssetTitle');
    $this->db->order_by('TitleCount','ASC');
    $this->db->limit(4);
    

    试试这个

    你试过了吗:
    $this->db->order_by('count(*)、'ASC')它工作!只需将
    ASC
    更换为
    DESC
    。谢谢@GordonLinoff您可能需要在选择中添加第二个参数
    FALSE
    。CodeIgniter将在诸如
    count(Title)
    之类的内容周围添加反勾号。