我正在尝试创建一个程序,在该程序中,给定一个战斗名称,生成在那场战斗中的两个国家。(如果不完全是两个国家,则为两个国家生成 NULL)
该程序的第一部分对我来说并不重要(我认为)
SELECT ship FROM outcomes WHERE battle = inBattle AS t1;
SELECT DISTINCT class FROM ships WHERE name IN t1 AS t2;
SELECT DISTINCT country FROM classes WHERE class IN t2;
我认为这会给我一个表格,其中列出了特定战斗中的不同国家。当我尝试做第二部分时,我的问题就出现了,如果不是完全找到两个国家,那么这两个国家都会产生 NULL。
CREATE PROCEDURE findCountriesInBattle( in inBattle VARCHAR(50) )
BEGIN
SELECT ship FROM outcomes WHERE battle = inBattle AS t1;
SELECT DISTINCT class FROM ships WHERE name IN t1 AS t2;
SELECT DISTINCT country FROM classes WHERE class IN t2 AS t3;
IF(SELECT COUNT(*) FROM t3 < 2 OR SELECT COUNT(*) FROM t3 > 2)
[show NULL, NULL]; << Line 1
ELSE
SELECT * FROM t3; << Line 2
END IF;
END;
//
我如何正确编写第 1 行和第 2 行?其他线路对吗?
关系:
classes(class, type, country, numGuns, bore, displacement)
ships( name, class, launched)
battles(name, date)
outcomes(ship, battle, result)