2013-08-17 75 views
-1

我試圖通過mysqli查詢獲取數據。 查詢看起來像:MySqli和PHP:不在聲明

SELECT * FROM pxldr_drawings 
      ORDER BY RAND() 
      WHERE id NOT IN (1,3,4,2) 
      LIMIT 1 

,但我得到了以下錯誤消息

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 'WHERE id NOT IN (1,3,4,2) LIMIT 1' at line 3 

我也試過NOT IN ('1','3','4','2')NOT IN(1,3,4,2),但既不工作。

感謝,LB

+0

你的ORDER BY和WHERE子句是錯誤的順序。將它們交換。 – andrewsi

回答

3

WHERE子句必須在ORDER BY子句之前。

試試這個

 SELECT * FROM pxldr_drawings 
      WHERE id NOT IN (1,3,4,2) 
      ORDER BY RAND() 
      LIMIT 1 
1

條款的順序是非常重要的,order by應該來where後。

SELECT * FROM pxldr_drawings 
      WHERE id NOT IN (1,3,4,2) 
      ORDER BY RAND() 
      LIMIT 1 

對於其中不同的條款需要正確的語法放置,請參考「Select Syntax」文檔。