如果列值提供空值,如何从MySQL中的表中检索所有数据?

时间:2015-04-21 07:10:47

标签: mysql select where mysql-workbench

如果列值提供空值,我想从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)); 

2 个答案:

答案 0 :(得分: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');

答案 1 :(得分:0)

null与空字符串('')不同,应使用is运算符明确检查。

假设您传递的变量是:country

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