0

I have a trouble getting a code working with CI especially that I need to embed a complex condition on the join I want to extract and count information from all fields in A even though not satisfying the condition which will result in zero 0 as count

First case that works great for count

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

Now if I want to add a condition on a row from A that is a datetime, so that we extract all information from A AFTER a given date, and same thing, if a condition is not satisfying we return 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');

This code is working but retruning only rows satisfying the condition and not all the other rows with 0 as count.

Can someone tell me what is wrong with the where clause ?

Thanks

4

1 回答 1

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;
于 2013-08-30T02:37:42.223 回答