带参数的 MySQL 函数出错

Error in MySQL function with parameters

我有 MySQL 函数,带有 2 个参数 user_idpost_id

这是我的函数:

CREATE FUNCTION isliked(pid INT, uid INT)
RETURN TABLE
AS
RETURN (EXISTS (SELECT 1 FROM likedata ld WHERE post_id = pid AND user_id = uid
       )) as is_liked
END

我尝试使用以下查询调用它:

SELECT posts.id, posts.title, isliked(111,123)
FROM posts

它 return 出现以下错误:

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 'RETURN TABLE
AS
RETURN (EXISTS (SELECT 1 FROM likedata ld WHERE post_id = pid AN' at line 2

应该是return这样的结果http://sqlfiddle.com/#!9/91040/5 我是 sql 的新手,任何帮助都将非常有用,在此先感谢

如果您希望函数为 return 布尔值,请使用:

CREATE FUNCTION isliked(pid INT, uid INT)
RETURNS BIT
   RETURN ( EXISTS ( SELECT 1 FROM likedata ld WHERE post_id = pid AND user_id = uid ) )