// full matches, respectively)
$where = array();
$on = array("SUBSTR(t1.{$this->rule_field}, 1, {$this->rule_length}) = SUBSTR(t2.{$this->rule_field}, 1, {$this->rule_length})");
- $using = array($this->rule_field);
+ $entity = CRM_Core_DAO_AllCoreTables::getBriefName(CRM_Core_DAO_AllCoreTables::getClassForTable($this->rule_table));
+ $fields = civicrm_api3($entity, 'getfields', ['action' => 'create'])['values'];
+
+ $innerJoinClauses = [
+ "t1.{$this->rule_field} IS NOT NULL",
+ "t2.{$this->rule_field} IS NOT NULL",
+ "t1.{$this->rule_field} = t2.{$this->rule_field}"
+ ];
+ if ($fields[$this->rule_field]['type'] === CRM_Utils_Type::T_DATE) {
+ $innerJoinClauses[] = "t1.{$this->rule_field} > '1000-01-01'";
+ $innerJoinClauses[] = "t2.{$this->rule_field} > '1000-01-01'";
+ }
+ else {
+ $innerJoinClauses[] = "t1.{$this->rule_field} <> ''";
+ $innerJoinClauses[] = "t2.{$this->rule_field} <> ''";
+ }
switch ($this->rule_table) {
case 'civicrm_contact':
case 'civicrm_address':
$id = 'contact_id';
$on[] = 't1.location_type_id = t2.location_type_id';
- $using[] = 'location_type_id';
+ $innerJoinClauses[] = ['t1.location_type_id = t2.location_type_id'];
if ($this->params['civicrm_address']['location_type_id']) {
$locTypeId = CRM_Utils_Type::escape($this->params['civicrm_address']['location_type_id'], 'Integer', FALSE);
if ($locTypeId) {
if ($this->rule_length) {
$where[] = "SUBSTR(t1.{$this->rule_field}, 1, {$this->rule_length}) = SUBSTR('$str', 1, {$this->rule_length})";
$where[] = "t1.{$this->rule_field} IS NOT NULL";
- $where[] = "t1.{$this->rule_field} <> ''";
}
else {
$where[] = "t1.{$this->rule_field} = '$str'";
$from = "{$this->rule_table} t1 JOIN {$this->rule_table} t2 ON (" . implode(' AND ', $on) . ")";
}
else {
- $from = "{$this->rule_table} t1 JOIN {$this->rule_table} t2 USING (" . implode(', ', $using) . ")";
+ $from = "{$this->rule_table} t1 INNER JOIN {$this->rule_table} t2 ON (" . implode(' AND ', $innerJoinClauses) . ")";
}
}
// finish building WHERE, also limit the results if requested
if (!$this->params) {
$where[] = "t1.$id < t2.$id";
- $where[] = "t1.{$this->rule_field} IS NOT NULL";
- $where[] = "t1.{$this->rule_field} <> ''";
}
$query = "SELECT $select FROM $from WHERE " . implode(' AND ', $where);
if ($this->contactIds) {