我想从Codeigniter上的模型修改以下查询:
public $table = 'fi_client_notes';
public $primary_key = 'fi_client_notes.client_note_id';
public function default_select()
{
$this->db->select("SQL_CALC_FOUND_ROWS fi_client_notes.*, fi_clients.client_name", FALSE);
if ($this->session->userdata('user_id') <> 1)
{
$this->db->where('fi_clients.user_id', $this->session->userdata('user_id'));
}
}
public function default_order_by()
{
$this->db->order_by('fi_client_notes.client_note_date DESC');
}
public function default_join()
{
$this->db->join('fi_clients', 'fi_clients.client_id = fi_client_notes.client_id');
}
我的目标是首先只选择值为&#34; fi_client_notes.client_note_end&#34;的行。等于今天的日期,然后根据值&#34; fi_client_notes.client_note_date&#34;显示按降序排列的所有其余行。
这是我尝试的查询:
(SELECT SQL_CALC_FOUND_ROWS fi_clients.client_name, fi_client_notes.* FROM (`fi_client_notes`) JOIN `fi_clients` ON `fi_clients`.`client_id` = `fi_client_notes`.`client_id` WHERE `fi_client_notes`.`client_note_end` = CURDATE())
UNION
(SELECT fi_clients.client_name, fi_client_notes.* FROM (`fi_client_notes`) JOIN `fi_clients` ON `fi_clients`.`client_id` = `fi_client_notes`.`client_id`
ORDER BY `fi_client_notes`.`client_note_date` DESC LIMIT 15)
但我不知道如何在Codeigniter上使用它,因为CodeIgniter的ActiveRecord不支持UNION
答案 0 :(得分:1)
在这种情况下,您可以获得没有联合的结果,只需将n.client_note_end <> CURDATE()
添加为order by子句的顶级
SELECT c.client_name, n.*
FROM `fi_client_notes` as n
JOIN
`fi_clients` as c
ON c.`client_id` = n.`client_id`
ORDER BY (n.`client_note_end` <> CURDATE()),
`n.`client_note_date` DESC LIMIT 15