sql:除非同时指定了 TOP,否则 ORDER BY 子句在视图、内嵌函数、派生表和子查询中无效。

3610 2004-09-22 08:04:53
DECLARE @id SMALLINT
SET @id=0
WHILE(@id<28)
BEGIN
IF(SELECT person_info.person_info_user_id,person_info.person_info_xingming,person_info.person_info_age,person_info.person_info_sex_example,person_jiaoyu.person_jiaoyu_xueli_example,person_jiaoyu.person_jiaoyu_zhuanye_name,person_gengxin.person_gengxin_indete FROM person_info,person_jiaoyu,person_gengxin ORDER BY person_gengxin.person_gengxin_id DESC) BEGIN
SET @id=@id+1
END
END

---------------------------------------------------------------
我sql学的不大好,大家帮一下,
我想从数据库中搜出28人的数据,
是按他们的更新时间来的
person_gengxin.person_gengxin_id在数据库中是一个一次递增1的bigint。
谁的时间更新了谁person_gengxin.person_gengxin_id的值就最大。
--------------------------------------------------------------------
: System.Data.SqlClient.SqlException: 除非同时指定了 TOP,否则 ORDER BY 子句在视图、内嵌函数、派生表和子查询中无效。

源错误:

执行当前 Web 请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪信息确定有关异常原因和发生位置的信息。

堆栈跟踪:


[SqlException: 除非同时指定了 TOP,否则 ORDER BY 子句在视图、内嵌函数、派生表和子查询中无效。]
System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream) +723
System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior) +45
System.Data.SqlClient.SqlCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior) +5
System.Data.Common.DbDataAdapter.FillFromCommand(Object data, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +304
System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +77
System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, String srcTable) +36
IndexData.Page_Load(Object sender, EventArgs e) +142
System.Web.UI.Control.OnLoad(EventArgs e) +67
System.Web.UI.Control.LoadRecursive() +35
System.Web.UI.Page.ProcessRequestMain() +725


...全文
2277 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
3610 2004-09-22
  • 打赏
  • 举报
回复
SELECT top 28

是什么意思?是不是搜出28个数据,
3610 2004-09-22
  • 打赏
  • 举报
回复
SELECT top 28 person_info.person_info_user_id,person_info.person_info_xingming,person_info.person_info_age,person_info.person_info_sex_example,person_jiaoyu.person_jiaoyu_xueli_example,person_jiaoyu.person_jiaoyu_zhuanye_name,person_gengxin.person_gengxin_date FROM person_info,person_jiaoyu,person_gengxin WHERE person_gengxin.person_gengxin_user_id=person_info.person_info_user_id AND person_gengxin.person_gengxin_user_id=person_jiaoyu.person_jiaoyu_user_id ORDER BY person_gengxin.person_gengxin_id DESC",connection

haole
3610 2004-09-22
  • 打赏
  • 举报
回复
SELECT person_info.person_info_user_id,
person_info.person_info_xingming,
person_info.person_info_age,
person_info.person_info_sex_example,
person_jiaoyu.person_jiaoyu_xueli_example,
person_jiaoyu.person_jiaoyu_zhuanye_name,
COUNT(person_gengxin.person_gengxin_id),
person_gengxin.person_gengxin_date

FROM person_info,person_jiaoyu,person_gengxin

WHERE person_gengxin.person_gengxin_user_id=person_info.person_info_user_id
AND
person_gengxin.person_gengxin_user_id=person_jiaoyu.person_jiaoyu_user_id

GROUP BY person_info.person_info_user_id,
person_info.person_info_xingming,
person_info.person_info_age,
person_info.person_info_sex_example,
person_jiaoyu.person_jiaoyu_xueli_example,
person_jiaoyu.person_jiaoyu_zhuanye_name,
person_gengxin.person_gengxin_id,
person_gengxin.person_gengxin_date

HAVING COUNT(person_gengxin.person_gengxin_id)=28

ORDER BY person_gengxin.person_gengxin_id DESC

我这么写一个都没查出来
Samen168 2004-09-22
  • 打赏
  • 举报
回复
写错误点,呵呵
ORDER BY person_gengxin_id DESC
Samen168 2004-09-22
  • 打赏
  • 举报
回复
Select * From (SELECT person_info.person_info_user_id,person_info.person_info_xingming,person_info.person_info_age,person_info.person_info_sex_example,person_jiaoyu.person_jiaoyu_xueli_example,person_jiaoyu.person_jiaoyu_zhuanye_name,person_gengxin.person_gengxin_indete FROM person_info,person_jiaoyu,person_gengxin) as tmpTable ORDER BY person_gengxin.person_gengxin_id DESC
Eddie005 2004-09-22
  • 打赏
  • 举报
回复
SELECT top 28 person_info.person_info_user_id,person_info.person_info_xingming,person_info.person_info_age,person_info.person_info_sex_example,person_jiaoyu.person_jiaoyu_xueli_example,person_jiaoyu.person_jiaoyu_zhuanye_name,person_gengxin.person_gengxin_indete FROM person_info,person_jiaoyu,person_gengxin ORDER BY person_gengxin.person_gengxin_id DESC
jietuan 2004-09-22
  • 打赏
  • 举报
回复
同样楼上所说的
3610 2004-09-22
  • 打赏
  • 举报
回复
在内联中是不是不能用ORDER BY

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧