2015-03-02 114 views
1

當我單獨執行這些查詢,我得到下面$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 | 
+0

爲什麼你的服務器投2個RES第二個結果是不壞,不知道ults如果用戶存在... – 2015-03-02 21:26:00

+0

我的猜測是,它因爲它的一個iSeries – 2015-03-02 21:35:55

回答

0

嘗試此查詢:

$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 "; // additional where clause 

你命名列和表中並沒有真正使得它更容易。但是這應該給你你正在尋找

這應該輸出如下結果:

CCCOMPANY CCCNTY UserOnSite 
AD  | AT | NULL    
AD  | BB | exist here 
AD  | BE | exist here 
AD  | BR | exist here 
AD  | CM | exist here 
AD  | CO | exist here 
AD  | DE | exist here 
AD  | EB | exist here 
AD  | EP | exist here 
AD  | FB | exist here 

其次更好的嘗試:編輯

$sql = " 
SELECT DISTINCT c.CCCOMPANY, c.CCCNTY, (
SELECT (
    CASE WHEN u.UCUSERID='$userid' 
    THEN 'exists here' 
    END 
) 
FROM D.TMTBCNTU as u 
WHERE u.UCUSERID = '$userid' 
AND u.UCCOMPANY = c.CCCOMPANY 
AND u.UCCNTY = c.CCCNTY 
LIMIT 1 
) AS UserOnSite 
FROM D.TMTBCNTC AS c 
WHERE c.CCCOMPANY = '$comp_code'"; 

這應該更好地工作

+0

即將結束,但它打印公司列表兩次,不包括用戶結果。 這是一箇舊的數據庫,我沒有任何選擇的列和表名稱。 – 2015-03-02 20:40:19

+0

向我顯示結果和你需要的信息 – 2015-03-02 20:42:39

+0

當我運行你的第二個查詢時,我得到這個:Warning:db2_fetch_array(): – 2015-03-02 21:21:13