2014-07-23 37 views
-1

我當前的腳本從數據庫中提取一個值,分解位置並將其輸入到Google的地理編碼API中。有沒有辦法查詢我的數據庫,以便空白值被跳過?

但是,一些組織(爲此我拉的位置)是空白的。例如,我將有

Org#1 Alabama 
Org#2 Kentucky 
Org#3 
Org#4 California 

我知道我可以使用if/else語句來過濾掉空項,但我想知道是否有辦法從我的數據庫查詢做到這一點,這樣額外的腳本時間不浪費只是篩選出空的條目。

我當前的查詢是這樣的:

$sql = "SELECT Name As org FROM Orgs WHERE Org=".$Org; // get organization that matches org ID 
$org = sql_return_one_number($db_connection, $sql); // magic 
    $orgrep = str_replace(" ", "+", $org); //remove all spaces and add a + sign for google API purposes 
$latlong = file_get_contents("https://maps.googleapis.com/maps/api/geocode/json?address=$orgrep&sensor=false&key=KEY"); // pass info to google 
$output=json_decode($latlong); //store it 
$status=$output->status; 
+0

是否所有的結果都來自分貝與 '組織#_'? –

+0

是的,組織編號或組織編號是我們所有組織的數字標識符。 Org ID與計數器和組織的最大組合一起使用,以便當腳本到達組織的最後組織時,該腳本將在組織#1處重新開始。 – lbaile200

+0

你不能只是和你的SQL的「和Org <>」'「嗎? – durbnpoisn

回答

1

您可以簡單地添加AND location NOT NULLAND location <> ''(取決於字段是否爲空或只是空)到您的查詢。

2

你會使用where條款,是這樣的:

where name is not null 
where name <> '' 
where name <> '' and name is not null 
where name is like 'Org#%' and name is not like 'Org#% %' 

據你所說的「空白」的意思還不清楚。第一個作品,如果坯NULL,第二,如果一個空字符串,第三如果NULL或空字符串,第四,如果名稱以'組織#」

1

只需添加另一個條件的WHERE條款開始:

SELECT Name AS org 
FROM Orgs 
WHERE Org = ? 
AND Name != '' 

如果空白,你的意思是NULL作者:

SELECT Name AS org 
FROM Orgs 
WHERE Org = ? 
AND Name IS NOT NULL 
1

你可以這樣做幾種不同的方法。爲你的查詢添加更多的子句,或者在將它傳遞給谷歌之前檢查$ orgrep。

SELECT Name AS org FROM Orgs WHERE Org=? AND (Name IS NOT NULL OR Name != '') 

OR

$sql = "SELECT Name As org FROM Orgs WHERE Org=".$Org; // get organization that matches org ID 
$org = sql_return_one_number($db_connection, $sql); // magic 
$orgrep = str_replace(" ", "+", $org); //remove all spaces and add a + sign for google API purposes 
if (!empty($orgrep)) { 
    $latlong = file_get_contents("https://maps.googleapis.com/maps/api/geocode/json?address=$orgrep&sensor=false&key=KEY"); // pass info to google 
    $output=json_decode($latlong); //store it 
    $status=$output->status; 
} 
相關問題