2015-04-21 28 views
-2

如果列值提供空值,我想從MySQL中的表中檢索所有數據?如果列值提供空值,如何檢索MySQL中的表中的所有數據?

我有一個像國家的表。 enter image description here

如何編寫一個查詢

  • 如果country = ' '我需要在表格中顯示的所有數據。
  • 如果country='algeria',它將顯示特定的數據。

我需要一個查詢中的兩個查詢。

i am getting result .but without providing variable how to write query... 
declare @country1 varchar(30) 
set @country1 = 'asd' 
    SELECT country_id, country 
FROM country 
WHERE ((@country1 = '') or (@country1 != '' and country.country= @country1)); 
+0

無論這些都無關NULL,你可能知道,當你想「我要這要這是真的做什麼。 ..「 –

+0

你如何將國家參數傳遞給查詢? – jarlh

+0

您可以使用case語句來說明您的條件 – Abhishek

回答

0

如果你有一個像:country輸入:

select 
    country.country_id, country.country 
from 
    country 
where 
    country.country = case when TRIM(:country) = '' or :country IS NULL then country.country else :country end 

DELIMITER $$ 

CREATE PROCEDURE sp1 (x VARCHAR(30)) 
BEGIN 
    SELECT 
     country.country_id, country.country 
    FROM 
     country 
    WHERE 
     country.country = CASE WHEN TRIM(x) = '' OR x IS NULL THEN country.country ELSE x END; 
END; 
$$ 

DELIMITER ; 

call sp1('COUNTRY'); 
+0

我正在使用MySQL工作臺。聲明$ country nvarchar(30)顯示錯誤。 –

+0

@Gorigeashokkumar我認爲[在MySQL Workbench中應該創建過程](http://stackoverflow.com/q/3981432/4519059);)。 –

+0

我得到的結果。但沒有提供變量如何編寫查詢... –

0

null s爲比空字符串('')不同,並應與is運營商明確檢查。

假設你傳遞變量是:country

SELECT country_id, country 
FROM country 
WHERE (:country IS NULL) or (:country = country.country) 
+0

我正在使用MySQL工作臺。如何傳遞:國家變量。當我使用聲明它顯示錯誤。 –

相關問題