douduanque5850 2017-03-28 15:21
浏览 20
已采纳

CodeIgniter查询不会返回正确的数据

So I have two tables customers and agenda

What I'm trying to get is all customers for user_id and their next appointment.

This is how far I got , but this keeps returning a wrong date

        $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()));

Here is a screenshot of the results

image

All these dates should be in the future otherways it should return NULL

Note : another small thing that might be causing this is that no matter how I join "left" "right" "inner" It keeps returning exactly the same results.

Thanks in advance.

  • 写回答

1条回答 默认 最新

  • dongmi4035 2017-03-28 15:26
    关注

    You need to quote your date in your query.

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

    Otherwise your date will be interpreted as a number, such as 2017-03-28 or 1986 (since that's what you get when you subtract).

    You can also do:

    $this->db->join('agenda a', "a.customer_id = c.customer_id AND a.date > '{$today}'", 'left');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 hexo+github部署博客
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?