2

我有带有2 个参数的 MySQL 函数,即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

它返回以下错误:

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 新手,任何帮助都会很棒,在此先感谢

4

1 回答 1

1

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

CREATE FUNCTION isliked(pid INT, uid INT)
RETURNS BIT
   RETURN ( EXISTS ( SELECT 1 FROM likedata ld WHERE post_id = pid AND user_id = uid ) )
于 2018-05-28T06:35:18.677 回答