mysql - ( 附近的语法错误

标签 mysql

我正在尝试查询一些东西,但我对 Mysql 不是很好,所以我想知道是否有人可以告诉我这里的问题是什么。这是我的表格:

 create table #transfers (
     sender varchar not null,
     recipient varchar not null,
          date date not null,
          amount integer not null
      );


INSERT INTO #transfers(sender,recipient,date,amount) VALUES ('Smith','Williams','2000-01-01',200);
INSERT INTO #transfers(sender,recipient,date,amount) VALUES ('Smith','Taylor','2002-09-27',1024);
INSERT INTO #transfers(sender,recipient,date,amount) VALUES ('Smith','Johnson','2005-06-26',512);
INSERT INTO #transfers(sender,recipient,date,amount) VALUES ('Williams','Johnson','2010-12-17',100);
INSERT INTO #transfers(sender,recipient,date,amount) VALUES ('Williams','Johnson','2004-03-22',10);
INSERT INTO #transfers(sender,recipient,date,amount) VALUES ('Brown','Johnson','2013-03-20',500);
INSERT INTO #transfers(sender,recipient,date,amount) VALUES ('Johnson','Williams','2007-06-02',400);
INSERT INTO #transfers(sender,recipient,date,amount) VALUES ('Johnson','Williams','2005-06-26',400);
INSERT INTO #transfers(sender,recipient,date,amount) VALUES ('Johnson','Williams','2005-06-26',200);

这是查询:

WITH cte AS
(
  SELECT *, rn = ROW_NUMBER() OVER (PARTITION BY recipient ORDER BY amount DESC)
  FROM #transfers
)
SELECT recipient
FROM cte
WHERE rn <= 3
GROUP BY recipient
HAVING SUM(amount) >= 1024
ORDER BY recipient

但是我得到这个错误:

在“(”附近:语法错误

最佳答案

CTEROW_NUMBER 在 MySQL 中不可用。

尝试使用子查询:

SELECT recipient
FROM 
(
     SELECT t.*, @rownum := @rownum + 1 AS rank
     FROM #transfers t, (SELECT @rownum := 0) r
)X
WHERE rank <= 3
GROUP BY recipient
HAVING SUM(amount) >= 1024
ORDER BY recipient

Fiddle Demo

关于mysql - ( 附近的语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36571856/

相关文章:

javascript - 从文本字段中抓取文本并用它更新数据库

sql - MySQL:是否可以返回 "mixed"数据集?

MySQL在一个具有依赖关系的事务中有两个插入语句

mysql - SQL 获取首先创建的金额最高的行

php - 格式化 REPLACE 查询的注释日期

mysql - mysql查询中单选的指定条件

java - 从 MySQL 获取数据到 Android 应用程序

mysql 从表中获取每个类别的最新文章

php - 使用 PHP 加载页面时如何在 mysql 中设置字段

php - MySQL 查询,使 ORDER BY rand() 将特定行放在底部