mysql函数中有参数时出错

yhxst69z  于 2021-06-23  发布在  Mysql
关注(0)|答案(1)|浏览(406)

我有一个mysql函数,有两个参数,即user\u id和post\u 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

它返回以下错误:

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

应该是这样的返回结果http://sqlfiddle.com/#!9/91040/5我是sql新手,任何帮助都会很好,提前谢谢

9rygscc1

9rygscc11#

如果希望函数返回布尔值,请使用:

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

相关问题