active recored where statement

时间:2015-07-17 19:02:07

标签: php mysql codeigniter activerecord

在codeigniter中,我试图生成以下SQL语句:

SELECT * 
FROM (`ea_users`,`ea_appointments`,`ea_services`) 
WHERE `ea_appointments`.`id_users_customer` = `ea_users`.`id` 
AND `ea_appointments`.`id_services` = `ea_services`.`id` 
AND `ea_appointments`.`start_datetime`> '2015-07-18 00:00:00'
AND `ea_appointments`.`start_datetime`< '2015-07-18 23:59:59'

在Active Record格式中我试过这个:

$day_start = date('Y-m-d 00:00:00', $day);
$day_end = date('Y-m-d 23:59:59', $day);
return $this->db->select('*')
  ->from('ea_appointments,ea_services,ea_users')
  ->where('ea_appointments.id_users_customer','ea_users.id') 
  ->where('ea_appointments.id_services','ea_services.id')
  ->where('ea_appointments.start_datetime>',$day_start)
  ->where('ea_appointments.start_datetime<',$day_end)
  ->get()->result();

但它产生了这个:

SELECT *
FROM (`ea_appointments`, `ea_services`, `ea_users`)
WHERE `ea_appointments`.`id_users_customer` =  'ea_users.id'
AND `ea_appointments`.`id_services` =  'ea_services.id'
AND `ea_appointments`.`start_datetime>` '2015-07-18 00:00:00'
AND `ea_appointments`.`start_datetime<` '2015-07-18 23:59:59'

我如何获得&#39; ea_users.id&#39;和&#39; ea_services.id&#39;译为&#39; ea_users&#39;。&#39; id&#39;和&#39; ea_services&#39;。&#39; id&#39;?我试过了:

  ->where('ea_appointments.id_users_customer','ea_users'.'id') 
  ->where('ea_appointments.id_services','ea_services'.'id')

但是这产生了这个:

WHERE `ea_appointments`.`id_users_customer` =  'ea_usersid'
AND `ea_appointments`.`id_services` =  'ea_servicesid'

正确的格式是什么?

3 个答案:

答案 0 :(得分:0)

这应该有效: (&gt;&lt; sign)之前的空格

$day_start = date('Y-m-d 00:00:00', $day);
$day_end = date('Y-m-d 23:59:59', $day);
return $this->db->select('*')
->from('ea_appointments,ea_services,ea_users')
->where('ea_appointments.id_users_customer','ea_users.id') 
->where('ea_appointments.id_services','ea_services.id')
->where('ea_appointments.start_datetime >',$day_start)
->where('ea_appointments.start_datetime <',$day_end)
->get()->result();

答案 1 :(得分:0)

假设您的原始sql函数在尝试在codeigniter中重新创建之前按预期工作,您可以简单地执行此操作。

$day_start = date('Y-m-d 00:00:00', $day);
$day_end = date('Y-m-d 23:59:59', $day);

$query = $this->db->query(SELECT * 
FROM (ea_users, ea_appointments, ea_services) 
WHERE ea_appointments.id_users_customer = ea_users.id 
AND ea_appointments.id_services = ea_services.id 
AND ea_appointments.start_datetime > {$day_start}
AND ea_appointments.start_datetime < {$day_end});

$query->result();

答案 2 :(得分:0)

这有效:

$day_start = date('Y-m-d 00:00:00', $day);
$day_end = date('Y-m-d 23:59:59', $day);
return $this->db->select('ea_appointments.id,
 ea_appointments.start_datetime,
 ea_appointments.end_datetime, 
 ea_users.email, 
 ea_services.name')
  ->from('ea_appointments')
  ->join('ea_users', 'ea_appointments.id_users_customer = ea_users.id','left') 
  ->join('ea_services', 'ea_appointments.id_services = ea_services.id','left')
  ->where('ea_appointments.start_datetime >',$day_start)
  ->where('ea_appointments.start_datetime <',$day_end)
  ->get()->result();

加入语句就可以了。