0

我有每个人的 2 个电话号码。有些只填写了电话号码,有些只填写了电话号码2,有些则两者都有。

我正在尝试查找具有相同电话号码的其他人,电话 = 电话,电话 = 电话 2,电话 2 = 电话,电话 2 = 电话 2

问题是,如果电话 1 为空,那么我现有的代码会将其与具有电话号码的下一个人匹配,而不管电话或电话 2 是否为空。我需要在代码中添加什么。

    function getothers($tid,$criteria,$telephone,$telephone2,$elector){
                global $dbh;
                $tid = '-TID'.$tid;
                 $sql = "SELECT * FROM electors 
WHERE (telephone > 0 OR telephone2 > 0)
AND records NOT RLIKE '$tid'
AND (telephone IN ('$telephone','$telephone2') 
OR telephone2 IN ('$telephone','$telephone2')) 
AND ID != '$elector'  LIMIT 10";
                return $dbh->query($sql);
            }
4

2 回答 2

1
SELECT T_el.*
FROM (electors AS T_el) LEFT JOIN (electors AS T_dup)
ON (T_el.ID!=T_dup.ID
  AND (
    (T_el.telephone IS NOT NULL AND (T_el.telephone=T_dup.telephone OR T_el.telephone=T_dup.telephone2))
    OR
    (T_el.telephone2 IS NOT NULL AND (T_el.telephone2=T_dup.telephone OR T_el.telephone2=T_dup.telephone2))
    )
)
WHERE T_dup.ID IS NOT NULL
GROUP BY T_el.ID
于 2012-07-02T12:04:54.240 回答
0
function getothers($tid,$criteria,$telephone,$telephone2,$elector){
            global $dbh;
            $tid = '-TID'.$tid;
             $sql = "SELECT * FROM electors WHERE (telephone > 0 AND telephone IN ('$telephone','$telephone2'))  OR (telephone2 > 0 AND telephone2 IN ('$telephone','$telephone2')) $criteria AND records NOT RLIKE '$tid' AND ID != '$elector'  LIMIT 10";
            return $dbh->query($sql);
        }
于 2012-07-02T12:05:39.837 回答