我有这样的功能:
DROP FUNCTION IF EXISTS user_repo //
CREATE FUNCTION user_repo(user_id INT) RETURNS VARCHAR(20)
BEGIN
DECLARE vote_value mediumint;
DECLARE score mediumint;
SELECT coalesce(sum(r.vote_value), 0), coalesce(sum(r.score), 0)
INTO vote_value, score
FROM reputations
WHERE owner_id = user_id
AND date_time > unix_timestamp(DATE_SUB(now(), INTERVAL 1 WEEK));
RETURN CONCAT_WS(',', vote_value, score);
END;//
它也有效。现在我还要将另一个参数传递给函数以确定时间范围。我的意思是我想传递一个字符串而不是WEEK
。这是我的新功能:
DROP FUNCTION IF EXISTS user_repo //
CREATE FUNCTION user_repo(user_id INT, range VARCHAR(10)) RETURNS VARCHAR(20)
BEGIN
DECLARE vote_value mediumint;
DECLARE score mediumint;
SELECT coalesce(sum(r.vote_value), 0), coalesce(sum(r.score), 0)
INTO vote_value, score
FROM reputations
WHERE owner_id = user_id
AND date_time > unix_timestamp(DATE_SUB(now(), INTERVAL 1 range));
RETURN CONCAT_WS(',', vote_value, score);
END;//
但它引发了这个错误:
以下查询失败:“CREATE DEFINER =
root
@localhost
FUNCTIONuser_repo
(user_id
INT,range
VARCHAR(10)CHARSET utf8) RETURNS VARCHAR(20)CHARSET utf8mb4 NOT DETERMINISTIC CONTAINS SQL SQL SECURITY DEFINER BEGIN DECLARE vote_value mediumint; DECLARE score mediumint; SELECT coalesce(sum(r.vote_value),0),coalesce(sum(r.score),0)INTO vote_value,得分来自声誉WHERE owner_id = user_id AND date_time> unix_timestamp(DATE_SUB(now(),INTERVAL 1 range)); RETURN CONCAT_WS(',',vote_value,score); END“ MySQL说:#1064 - 你的SQL语法有错误;检查与MariaDB服务器版本对应的手册,以便在'range)附近使用正确的语法); RETURN CONCAT_WS(',',vote_value,score);在第9行结束
有人有什么不对吗?