6

我一直在尝试声明一个整数变量,但它只是不起作用。这是我的查询:

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

4 回答 4

5

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

SET @count = 5633;

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

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

于 2011-11-10T13:57:34.943 回答
1
DECLARE @count INT;
SET     @count = 5633;

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

此外,您显然需要一个开始/结束块。

于 2011-11-10T13:39:39.757 回答
0

How about doing it all in-line

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

but more direct would otherwise, just have it directly in the WHERE clause.

于 2011-11-10T13:47:33.320 回答
-1
DECLARE @count INT
SET     @count = 5633

SELECT count(matchid) 
FROM    matches  //<===  should be table name
WHERE   id = @count
于 2011-11-10T13:37:26.397 回答