所以我有两个表customersagenda

我想要获得的是user_id及其下一个约会的所有客户。

这就是我走的路,但是这总是返回错误的日期

        $today = date("Y-m-d");

        $this->db->select('c.firstname,c.lastname,a.date as next_appointment');
        $this->db->from('customers c');
        $this->db->join('agenda a','a.customer_id = c.customer_id AND a.date > '.$today.'','left');
        $this->db->where('c.user_id', $user_id, FALSE);
        $this->db->group_by('c.customer_id');
        $this->db->order_by('c.firstname', 'asc');

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

        die(json_encode($Q->result_array()));


这是结果的屏幕截图



所有这些日期应在将来,否则应返回NULL

注意:另一个可能导致此问题的小事情是,无论我如何加入"left" "right" "inner",它都会始终返回完全相同的结果。

提前致谢。

最佳答案

您需要在查询中引用您的日期。

$this->db->join('agenda a', 'a.customer_id = c.customer_id AND a.date > "'.$today.'"', 'left');


否则,您的日期将被解释为数字,例如2017-03-281986(因为这是您减去时得到的)。

您也可以这样做:

$this->db->join('agenda a', "a.customer_id = c.customer_id AND a.date > '{$today}'", 'left');

09-10 00:30
查看更多