2014-07-10 53 views
1
"select top 5 db_statesid 
    from seo_states 
    where db_statesid in (" + 
      "SELECT TOP 5 db_statesid 
       FROM (SELECT DISTINCT a.db_statesid 
         FROM seo_states a 
         where country_id=" + country_id + ") 
       ORDER BY RND(db_statesid))"; 

我正在使用此查詢進行訪問,它工作正常,但不適用於在Order by附近給出語法錯誤的sql。SQL查詢通過給出錯誤的順序

+0

是'RND'在訪問一個圓形或隨機函數?什麼是您收到的*** FULL ***錯誤? – Kermit

+0

關鍵字'ORDER'附近的語法錯誤。 –

+0

隨機函數 –

回答

2

在sql中,所有派生表都必須是別名。注:將t1ORDER BY

"select top 5 db_statesid 
    from seo_states 
    where db_statesid in (" + 
      "SELECT TOP 5 db_statesid 
       FROM (SELECT DISTINCT a.db_statesid 
         FROM seo_states a 
         where country_id=" + country_id + ") t1 
       ORDER BY RND(db_statesid))" 

編輯

除非RND是一個自定義功能,你可能想RAND

+1

非常感謝你 –

+0

'RND'不是SQL Server中的有效函數 – Kermit