mysql函数中有参数时出错

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

我有一个mysql函数,有两个参数,即user\u id和post\u id
我的职责是:

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

我试着用下面的问题来称呼它:

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

它返回以下错误:

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

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

9rygscc1

9rygscc11#

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

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

相关问题