在 MySQL 中创建存储过程

Create Stored Procedure in MySQL

我在 MySQL 5.0.10

中使用以下语法创建存储过程
delimiter //
CREATE PROCEDURE getTweets (var1 varchar(100))
LANGUAGE SQL
SQL SECURITY DEFINER
COMMENT 'A procedure to return 20 least scored tweets based on the temp sessionid of the user'
BEGIN
    SELECT count(ts.tweetid) as "count",t.id as "tweetid", t.tweettext as "tweet"
    FROM tweets t
    LEFT JOIN tweetscores ts ON t.id = ts.tweetid
    where t.id not in (select distinct(tweetid) from tweetscores where  temp_sessionid=var1)
    group by t.id, t.tweettext order by count   
    LIMIT 10;
END//

我一直收到错误

#1064 - 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 '//' at line 12

任何人都可以发现错误..

您的查询在数据库级别作为脚本执行时有效。

但是使用 PhpMyAdmin 您需要删除 delimiter 语句。

CREATE PROCEDURE getTweets (var1 varchar(100))
LANGUAGE SQL
SQL SECURITY DEFINER
COMMENT 'A procedure to return 20 least scored tweets based on the temp sessionid of the user'
BEGIN
    SELECT count(ts.tweetid) as "count",t.id as "tweetid", t.tweettext as "tweet"
    FROM tweets t
    LEFT JOIN tweetscores ts ON t.id = ts.tweetid
    where t.id not in (select distinct(tweetid) from tweetscores where  temp_sessionid=var1)
    group by t.id, t.tweettext order by count   
    LIMIT 10;
end

PhpMyAdmin 将为您完成剩下的工作。

CREATE PROCEDURE getTweets (var1 varchar(100))
LANGUAGE SQL
SQL SECURITY DEFINER
COMMENT 'A procedure to return 20 least scored tweets based on the temp sessionid of the user'
BEGIN
    SELECT count(ts.tweetid) as "count",t.id as "tweetid", t.tweettext as "tweet"
    FROM tweets t
    LEFT JOIN tweetscores ts ON t.id = ts.tweetid
    where t.id not in (select distinct(tweetid) from tweetscores where  temp_sessionid=var1)
    group by t.id, t.tweettext order by count   
    LIMIT 10;
end