Sql查询通过给出错误来命令

时间:2014-07-10 00:05:31

标签: sql sql-server

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

1 个答案:

答案 0 :(得分:2)

在sql中,所有派生表都必须是别名。请注意在t1

之前添加ORDER 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