2015-05-13 90 views
1

我目前正在運行兩個查詢來查找一個結果,我有一個很好的感覺,我可以將這兩個查詢合併爲一個。我已經嘗試了下面的方法,但它仍然向我展示從access_number表中已經出現的結果。將2個Mysql查詢合併爲一個

$query = "select * FROM `$table`.`access_number` WHERE `active`='1'"; 
$result = mysql_query($query,$db) or die(mysql_error()); 
while($row = mysql_fetch_array($result)) { 
    list($dd_inuse) = mysql_fetch_row(mysql_query("SELECT `did` FROM `$table`.`customer_dd` WHERE `did`='$row[did]' AND `callerid`='$callerid' LIMIT 1",$db)); 
    if(!$dd_inuse) { 
    $goodone = $row['did']; 
    break; 
    } 
} 

我試着像這樣結合它,它沒有顯示我唯一值

select `access_number`.`did` from `access_number` 
INNER JOIN `customer_dd` 
WHERE `customer_dd`.`callerid`='$callerid' 
AND `customer_dd`.`did` !=`access_number`.`did` LIMIT 1 

底線是,我試圖找到表access_number不會在customer_dd

存在的價值

任何形式的幫助,我可以得到這是非常感謝!

+0

您需要從第二個結果的第一批成果,結果包裹一段時間。 –

+0

我希望只使用一個查詢,而不是在第一次獲取後進行循環... – thevoipman

回答

1

您可以使用left joinnot null這個

select 
a.access_number from access_number a 
left join customer_dd c on c.did = a.did and c.callerid = '$callerid' 
where c.did is null 
0

代碼:你的代碼

SELECT `did` FROM `$table`.`customer_dd` 
WHERE `did`='$row[did]' AND `callerid` = '$callerid' LIMIT 1 

基地,我發現你的表access_number.did = customer_dd.did

所以你可以嘗試:

select `access_number`.`did` from `access_number` 
INNER JOIN `customer_dd` ON `customer_dd`.`did` = `access_number`.`did` 
WHERE `customer_dd`.`callerid` = '$callerid' AND `access_number`.`active`='1' LIMIT 1 
+0

這與我想要的相反......我想找到一個不在'customer_dd'中的'access_number' – thevoipman