1

我正在尝试将此 SQL 查询转换为 codeigniter 查询

SELECT 
  uploads.EMAIL
FROM
  uploads 
  JOIN (

    SELECT EMAIL, COUNT(*) as num FROM uploads GROUP BY EMAIL
  ) c ON uploads.EMAIL = c.EMAIL
ORDER BY 
  c.num DESC, 
  EMAIL ASC

感谢您的帮助

4

1 回答 1

1

我不确定为什么您无法使用活动记录文档自己解决这个问题,但是:

$this->db->select('uploads.EMAIL');
$this->db->from('uploads');
$this->db->join('(SELECT EMAIL, COUNT(*) as num FROM uploads GROUP BY EMAIL) c','uploads.EMAIL = c.EMAIL','',FALSE);
$this->db->order_by('c.num desc, uploads.EMAIL asc');

接着

$query = $this->db->get();

仅供参考,将 FALSE 作为第四个参数传递给db->join()方法将导致它不会转义语句,因此如果要使用外部变量,则应小心。在 CodeIgniter 3 之前,这是使用活动记录进行子查询而不扩展活动记录类来添加它们的唯一方法。

于 2012-09-07T17:31:24.570 回答