【问题标题】:Active Record is not displaying correct queryActive Record 没有显示正确的查询
【发布时间】:2014-03-24 14:23:36
【问题描述】:

谁能告诉我为什么我无法在 codeigniter 中使用活动记录显示以下查询。我想我的 select 语句中的 TIMEDIFF 有问题,因为 TOTAL 和 FROM 之间没有空格

这是我的活动记录查询

$this->db->select('s.id, s.person_id, p.first_name, p.last_name, sch.id, sch.start_date, sch.start_hour, sch.end_hour, TIMEDIFF(sch.end_hour, sch.start_hour) AS total');
$this->db->join('staff s', 'sch.staff_id = s.id', 'left');
$this->db->join('persons p', 's.person_id = p.id', 'left');
$this->db->where('sch.start_date >=', $start);
$this->db->where('sch.start_date <=', $end);
$this->db->limit(10);

$query = $this->db->get('work_schedule sch');

这是打印出来的

SELECT `s`.`id`,
   `s`.`person_id`,
   `p`.`first_name`,
   `p`.`last_name`,
   `sch`.`id`,
   `sch`.`start_date`,
   `sch`.`start_hour`,
   `sch`.`end_hour`,
TIMEDIFF(sch.end_hour,`sch`.`start_hour)` AS totalFROM (`work_schedule` sch)
LEFT JOIN `staff` s ON `sch`.`staff_id` = `s`.`id`
LEFT JOIN `persons` p ON `s`.`person_id` = `p`.`id`
WHERE `sch`.`start_date` >= '2014-02-19'
  AND `sch`.`start_date` <= '2014-02-20'LIMIT 10

【问题讨论】:

    标签: php mysql codeigniter


    【解决方案1】:

    你可以试试。

    $query = $this->db->select('s.id, s.person_id, p.first_name, p.last_name, sch.id, sch.start_date, sch.start_hour, sch.end_hour, TIMEDIFF(sch.end_hour, sch.start_hour) AS total', FALSE)
                      ->from('work_schedule sch')
                      ->join('staff s', 'sch.staff_id = s.id', 'left')
                      ->join('persons p', 's.person_id = p.id', 'left')
                      ->where('sch.start_date >=', $start)
                      ->where('sch.start_date <=', $end)
                      ->limit(10)
                      ->get();
    
    $result = $query->result();
    

    【讨论】:

    • 什么是错误?我忘了在get() 后面加分号。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-17
    • 2013-02-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多