【问题标题】:Join with time comparison加入时间比较
【发布时间】:2013-08-30 02:35:13
【问题描述】:

我无法让代码与 CI 一起使用,尤其是我需要在联接中嵌入复杂条件 我想从 A 中的所有字段中提取和计算信息,即使不满足将导致零 0 作为计数的条件

第一个非常适合计数的案例

$this->db->distinct();
$this->db->select('A.id, count(B.id) AS C');
$this->db->from('A');
$this->db->join('B','B.id=A.id','left outer');
$this->db->group_by('A.id');

现在,如果我想在 A 的一行中添加一个条件,即日期时间,以便我们在给定日期之后从 A 中提取所有信息,同样,如果条件不满足,我们返回 0:

$this->db->distinct();
$this->db->select('A.id, count(B.id) AS C');
$this->db->from('A');
$this->db->join('B','B.id=A.id','left outer');
$this->db->where('B.date >',$date);
$this->db->group_by('A.id');

此代码正在运行,但仅重新计算满足条件的行,而不是所有其他以 0 为计数的行。

谁能告诉我 where 子句有什么问题?

谢谢

【问题讨论】:

  • 你的 $date 是 2013-08-30 09:00:00 这个格式吗?我的意思是 YYYY-MM-DD HH:MM:SS 格式?
  • 是的,我从 mysql DATETIME 格式中提取日期并进行比较

标签: sql codeigniter join count


【解决方案1】:

where 正在撤消left outer join。你能做到吗?

$this->db->join('B','B.id=A.id and 'B.date >', $date, 'left outer');

在 SQL 中,您可以通过将两个条件都放在 on 语句中来处理这个问题:

from A left outer join
     B
     on B.id = A.id and B.date > $date;

【讨论】:

  • 我也试过了,但由于某种原因,日期比较效果不佳,我有一个计数,但对于所有行,尽管有一个日期过滤器!
猜你喜欢
  • 2015-09-18
  • 2011-01-19
  • 2012-12-29
  • 1970-01-01
  • 2012-03-26
  • 2011-08-13
  • 2015-12-12
  • 2011-09-03
相关资源
最近更新 更多