2012-08-29 165 views
1

我收到一個語法錯誤,有人可以告訴我爲什麼?SQL子查詢中的語法錯誤

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 Error (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 '1 previousbalance FROM invoice i WHERE i.client = c.clientid ORDER BY i.invoicei' at line 1 */

+1

請在這裏展示的語法錯誤。和哪個SQL(MySQL,SQL Server等)? – hims056

+0

HediSQL是程序,SQL,這裏是錯誤'SQL錯誤(1064):你的SQL語法錯誤;檢查對應於您的MySQL服務器版本的手冊,以找到正確的語法,以便在第1行的'1 previousbalance FROM invoice i WHERE i.client = c.clientid ORDER BY i.invoicei'* /' – user1269625

回答

3

只是猜測,但它可能表明,你應該LIMIT 1WHERE ROWNUM < 2 LIMIT 1更換TOP 1。你使用什麼樣的數據庫?

+0

LIMIT 1不起作用。 ..how會在哪裏ROMNUM <2看起來像? – user1269625

+0

這不起作用? '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 FROM FROM client c ORDER BY clientname' – tbl

+0

thanks tbk! !!!!! – user1269625

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 
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 

See this SQLFiddle