我有MySQL函数,有两个参数,即user_id和post_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的新手,任何帮助都会很棒,提前感谢
如果希望函数返回布尔值,请使用:
CREATE FUNCTION isliked(pid INT, uid INT)
RETURNS BIT
RETURN ( EXISTS ( SELECT 1 FROM likedata ld WHERE post_id = pid AND user_id = uid ) )