在MySQL中创建存储过程

时间:2015-01-21 08:33:42

标签: mysql phpmyadmin

我有以下语法在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 - 您的SQL语法出错;       检查与您的MySQL服务器版本对应的手册,以便在' //'附近使用正确的语法。在第12行

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

2 个答案:

答案 0 :(得分:3)

您的查询在数据库级别上作为脚本执行时会起作用。

但是使用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将为您完成剩下的工作。

答案 1 :(得分:0)

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