无法声明变量SQL

时间:2011-11-10 13:36:12

标签: mysql sql variables declare

我一直试图声明一个整数变量,但它只是不起作用。这是我的疑问:

DECLARE @count INT
SET     @count = 5633

SELECT count(matchid) 
FROM   `matches`
WHERE   id = @count

我收到此错误:

Error Code: 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 'DECLARE @count INT
SET     @count = 5633

请帮助:)

4 个答案:

答案 0 :(得分:2)

根据MySQL manual,DECLARE只允许在BEGIN ... END块内,并且必须在开头。你也忘记了每一行末尾的分号。这应该适合你:

SET @count = 5633;

SELECT count(*) 
FROM matches
WHERE id = @count;

COUNT(*)在某些情况下会更快。

答案 1 :(得分:1)

DECLARE @count INT;
SET     @count = 5633;

SELECT count(matchid) 
FROM   matches
WHERE   id = @count;

此外,您显然是need一个开始/结束块。

答案 2 :(得分:0)

如何在线完成

SELECT
      count(matchid) 
   FROM   
      `matches`,
      ( select @count := 5633 ) SQLVars
   WHERE   
      matches.id = @count

但更直接的是,只需将它直接放在WHERE子句中。

答案 3 :(得分:-1)

DECLARE @count INT
SET     @count = 5633

SELECT count(matchid) 
FROM    matches  //<===  should be table name
WHERE   id = @count
相关问题