我正在使用带Codeigniter的Potgresql,并且我有一个查询,它在我需要用于order_by()
的列中返回空值。正如我在SO和谷歌上看到的许多例子一样,它们都不起作用。当我在pgAdmin中尝试这个查询时,它可以工作,并在最后一位上输入空值
SELECT a.*, b.*, c.*
FROM accounts a
LEFT JOIN subscriptions b ON a.id = b.provider_account_id AND b.subscriber_account_id = 10
JOIN users c ON c.email = a.name
WHERE a.user_role = 0
ORDER BY b.following DESC NULLS LAST, b.last_updated DESC NULLS LAST;
这是代码点火器中的模型中的查询,它将空值放在第一位上
$this->db->select('a.*, b.*, c.*');
$this->db->from('accounts a');
$this->db->join('subscriptions b', 'a.id = b.provider_account_id AND b.subscriber_account_id = '.$client_id, 'left');
$this->db->join('users c', 'c.email = a.name');
$this->db->where('a.user_role', 0);
$this->db->order_by('b.following', 'desc');
$this->db->order_by('b.last_updated', 'desc');
那么我如何在order_by()
函数中实现NULLS LAST
已解决
改进的CCD_ 4函数;system/database/DB_query_builder.php";
更改
$direction = in_array($direction, array('ASC', 'DESC'), TRUE) ? ' '.$direction : '';
至
$direction = in_array($direction, array('ASC', 'DESC', 'ASC NULLS FIRST', 'ASC NULLS LAST', 'DESC NULLS FIRST', 'DESC NULLS LAST'), TRUE) ? ' '.$direction : '';
现在我可以像一样使用它了
$this->db->order_by('column', 'desc nulls first');
$this->db->order_by('column', 'desc nulls last');
$this->db->order_by('column', 'asc nulls first');
$this->db->order_by('column', 'asc nulls last');