繁体   English   中英

(附近的语法错误

[英]Syntax error near (

我正在尝试查询某些内容,但我对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

但是我得到这个错误:

在“(”附近:语法错误

CTEsROW_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

小提琴演示

使用此创建表查询

create table transfers (
id INT NOT NULL AUTO_INCREMENT,
     sender VARCHAR(100) NOT NULL,
     recipient VARCHAR(100) NOT NULL,
          date DATE, 
          amount VARCHAR(100) NOT NULL,
          PRIMARY KEY ( id )
      );

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM