2014-07-10 31 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