所以我试图在mysql中创建我的第一个简单函数,它计算3个表中的行数并返回3个计数的总和。我认为这很简单,但显然不是每次我尝试创建它时都会出错。我不知道为什么,因为它看起来像我已经正确地声明了一切,并在需要时使用了正确的语法。但我仍然不明白为什么我得到这个错误。有什么想法吗?
我的功能
mysql> create function Size()
-> returns int
-> deterministic
-> begin
-> declare size int;
-> set size = select count(r.name) + count(s.name) + count(j.name) as Total from ArmyRegiment r, ArmySpecRegiment s, ArmyJTF j, ArmySpecRegiment s, ArmyJTF j;
-> return size;
-> end//
错误:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'select count(r.name) + count(s.name) + count(j.name) as Total from ArmyRegiment' at line 1
此外,我在输入结束之前立即输入此错误//在我可以执行分隔符之前; 。无论如何,提前谢谢。
答案 0 :(得分:0)
return int(100000)
应为returns int(100000)
。请注意它是RETURNS
。而且,我认为你可以说returns int
。
再次,我想你的SET
陈述;您正试图将count()
结果加起来
set size = select count(r.name) + count(s.name) + count(j.name)
from ArmyRegiment r, ArmySpecRegiment s, ArmyJTF j;
(OR)
declare size int;
set size = 0;
select count(name) into size from ArmyRegiment;
select size + count(name) into size from ArmySpecRegiment;
select size + count(name) into size from ArmyJTF;
(OR)
select count(r.name) + count(s.name) + count(j.name) INTO size
from ArmyRegiment r, ArmySpecRegiment s, ArmyJTF j;
答案 1 :(得分:0)
您不能像往常一样在多个列中使用SUM 如果你想要添加这些个人计数,你需要自己添加
select count(r.name) + count(s.name) + count(j.name) as summed from ArmyRegiment r, ArmySpecRegiment s, ArmyJTF j;
但是我怀疑这是你真正想要的结果,因为你似乎在做三个表的完全外连接,所以你将得到countR * countS * countJ的结果