1

我收到语法错误,谁能告诉我为什么?

SELECT  c.clientid, c.clientname, c.billingdate, 
      (SELECT TOP 1 previousbalance FROM invoice i 
          WHERE i.client = c.clientid ORDER BY i.invoiceid DESC) AS remaining 
FROM client c 
ORDER BY clientname

二级选择正在做的是clientid在发票表中获取最新记录。

程序 - HediSQl

SQL

这是错误:

SQL 错误 (1064):您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的“1 previousbalance FROM invoice i WHERE i.client = c.clientid ORDER BY i.invoicei”附近使用正确的语法 */

4

3 回答 3

3

只是猜测,但它可能表明您应该替换TOP 1LIMIT 1or WHERE ROWNUM < 2 LIMIT 1。你用的是什么类型的数据库?

于 2012-08-29T05:32:54.263 回答
1

你只需要像这样LIMIT使用TOP

SELECT  c.clientid, c.clientname, c.billingdate, 
   (SELECT previousbalance FROM invoice i 
     WHERE i.client = c.clientid ORDER BY i.invoiceid DESC LIMIT 1) AS remaining 
FROM client c 
ORDER BY clientname

看到这个 SQLFiddle

于 2012-08-29T05:39:25.250 回答
0

您拥有的语法将在 SQL Server 中工作。您的错误消息来自 MySQL。

尝试这个:

SELECT c.clientid, 
       c.clientname, 
       c.billingdate, 
       (
       SELECT previousbalance 
       FROM invoice i 
       WHERE i.client = c.clientid 
       ORDER BY i.invoiceid DESC
       LIMIT 1  
       ) AS remaining 
FROM client c 
ORDER BY clientname
于 2012-08-29T05:34:47.413 回答