我想使用像这样的MySQL查询:
SELECT * FROM COUNTRY_TABLE WHERE COUNTRY='INDIA'
它将包含国家/地区的数据的结果作为INDIA,但是当表中不存在国家/地区INDIA时,它应该在单个查询中给出全局或总结果的结果。
答案 0 :(得分:0)
Set @param = null;
SELECT *
FROM your_table
WHERE country = CASE
WHEN @param IS NULL THEN country
ELSE @param
END;
在SQL Fiddle上工作:http://www.sqlfiddle.com/#!2/da2d82/2
另一种解决方案:
SELECT *
FROM your_table
WHERE country = COALESCE(@param, country);
答案 1 :(得分:0)
试试这个
DROP PROCEDURE IF EXISTS ConditionalSelect;
DELIMITER $$
CREATE PROCEDURE `ConditionalSelect`()
BEGIN
SELECT COUNT(*) INTO @varCount FROM Country WHERE `name` = 'India';
IF @varCount >=1 THEN
SELECT * FROM Country WHERE `name` = 'India';
ELSE
SELECT * FROM Country;
END IF;
END$$
DELIMITER ;