當我單獨執行這些查詢,我得到下面$sql1->first
2列,$sql2->second
2列結果:MySQL查詢,比較表reseults
$userid='AD0004';
$comp_code='AD';
$sql1 = "SELECT DISTINCT CCCOMPANY, CCCNTY
FROM D.TMTBCNTC
WHERE CCCOMPANY='$comp_code'";
// //gets USER & their counties
$sql2 = "SELECT DISTINCT UCCOMPANY, UCCNTY
FROM D.TMTBCNTU
WHERE UCUSERID='$userid'
AND UCCOMPANY='$comp_code'";
CCCOMPANY CCCNTY UCCNTY UCCOMPANY
AD AT
AD BB BB AD
AD BE BE AD
AD BR BR AD
AD CM CM AD
AD CO CO AD
AD DE DE AD
AD EB EB AD
AD EP EP AD
AD FB FB AD
AD GB
AD GU GU AD
AD GV GV AD
AD HA HA AD
AD KF KF AD
AD KN KN AD
AD MB MB AD
AD MG MG AD
AD PF PF AD
AD TA TA AD
AD UB UB AD
AD WI WI AD
我想要做的就是加入了列CCCOMPANY=UCCOMPANY && CCCNTY=UCCNTY
並返回沒有找到匹配項時爲空白字段。
我遇到的問題是查詢似乎環回通過本身,這意味着我得到每家公司22個縣,每個用戶20個縣,其中就出來20x22 => 440分的結果,我想22分的結果和一個查詢。
EDIT1:
$sql = "
SELECT DISTINCT c_t.CCCOMPANY, c_t.CCCNTY, (
CASE WHEN u_t.UCUSERID='$userid'
THEN 'exists here'
END
) AS UserOnSite
FROM D.TMTBCNTC as c_t
LEFT JOIN D.TMTBCNTU AS u_t ON u_t.UCCOMPANY = c_t.CCCOMPANY
WHERE c_t.CCCOMPANY = '$comp_code'";
輸出:
AD | AT |
AD | BB |
AD | BE |
AD | BR |
AD | CM |
AD | CO |
AD | DE |
AD | EB |
AD | EP |
AD | FB |
AD | GB |
AD | GU |
AD | GV |
AD | HA |
AD | KF |
AD | KN |
AD | MB |
AD | MG |
AD | PF |
AD | TA |
AD | UB |
AD | WI |
AD | AT | exists here
AD | BB | exists here
AD | BE | exists here
AD | BR | exists here
AD | CM | exists here
AD | CO | exists here
AD | DE | exists here
AD | EB | exists here
AD | EP | exists here
AD | FB | exists here
AD | GB | exists here
AD | GU | exists here
AD | GV | exists here
AD | HA | exists here
AD | KF | exists here
AD | KN | exists here
AD | MB | exists here
AD | MG | exists here
AD | PF | exists here
AD | TA | exists here
AD | UB | exists here
AD | WI | exists here
EDIT2:
$sql = "
SELECT DISTINCT c_t.CCCOMPANY, c_t.CCCNTY, (
CASE WHEN u_t.UCUSERID='$userid'
THEN 'exists here'
END
) AS UserOnSite
FROM D.TMTBCNTC as c_t
LEFT JOIN D.TMTBCNTU
AS u_t
ON u_t.UCCOMPANY = c_t.CCCOMPANY
WHERE c_t.CCCOMPANY = '$comp_code'
AND u_t.UCCNTY = c_t.CCCNTY";
AD | AT |
AD | BB | exists here
AD | BB |
AD | BE | exists here
AD | BE |
AD | BR | exists here
AD | BR |
AD | CM | exists here
AD | CM |
AD | CO | exists here
AD | CO |
AD | DE | exists here
AD | DE |
AD | EB | exists here
AD | EB |
AD | EP | exists here
AD | EP |
AD | FB | exists here
AD | FB |
AD | GB |
AD | GU | exists here
AD | GU |
AD | GV | exists here
AD | GV |
AD | HA | exists here
AD | HA |
AD | KF | exists here
AD | KF |
AD | KN | exists here
AD | KN |
AD | MB | exists here
AD | MB |
AD | MG | exists here
AD | MG |
AD | PF | exists here
AD | PF |
AD | TA | exists here
AD | TA |
AD | UB | exists here
AD | UB |
AD | WI | exists here
AD | WI |
爲什麼你的服務器投2個RES第二個結果是不壞,不知道ults如果用戶存在... – 2015-03-02 21:26:00
我的猜測是,它因爲它的一個iSeries – 2015-03-02 21:35:55