如果列值提供空值,我想从MySQL中的表中检索所有数据吗?
我有一张像国家一样的桌子。
如何撰写查询
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 :(得分: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)