gpt4 book ai didi

c# - 从sql数据库中选择最新的3条记录

转载 作者:行者123 更新时间:2023-11-30 21:59:21 25 4
gpt4 key购买 nike

我的表名称是 tblEvent,它的列是 EventIDNameDescriptionEventTypeID TotalBudgetCustomerIDEventStatusEventDate

我要选取最近的3条记录。

我试过这个:

public DataTable HomeEvents()
{
string query = "select TOP 3 tblEvent.*, tblCustomer.Name as 'CustomerName', tblCustomer.Photo AS 'CustomerPhoto' from tblEvent ORDER BY EventID DESC, tblCustomer where tblEvent.CustomerID = tblCustomer.CustomerID";

List<SqlParameter> lstParams = new List<SqlParameter>();
DataTable dt = DBUtility.SelectData(query, lstParams);
return dt;
}

最佳答案

order by 子句应该在 where 子句之后:

select top 3
tblEvent.*,
tblCustomer.Name as 'CustomerName',
tblCustomer.Photo AS 'CustomerPhoto'
from
tblEvent
where
tblEvent.CustomerID = tblCustomer.CustomerID
order by
EventID desc,
tblCustomer

注意:如果 EventID 是自动递增的(主键,身份)并且记录实际上是按照它们发生的顺序创建的,那么该字段将随着时间的推移而递增。否则,您将需要使用 EventDate 字段进行排序(如 Tareq Alothman 所建议的那样)。

关于c# - 从sql数据库中选择最新的3条记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29315493/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com