2011-03-14 19 views
0

想弄清楚如何更好地从数据库中检索数据,而无需性能成本。仅检索article_tbl中的主键列号并检查?

计划如下:

  1. Select id from article table;
  2. 店铺ID在List<int> arr;
  3. 找出最后一篇文章ID。 int x = arr.Count()
  4. Select * from article_tbl where id = x;运行查询。
  5. 张贴在你的页面上。

我的计划是对的吗?从数据库中检索数据的更好方法是什么?

非常感谢

+0

你只是想选择最新文章,你在用什么数据库? – 2011-03-14 16:13:12

+0

是的,它可以解决,如“按日期DESC从article_tbl顺序选择top 1”。谢了哥们。但是,从数据库中检索数据的更好方法是什么?我正在使用SQLServer Express 2008 – user610961 2011-03-14 16:22:03

回答

0

尝试是这样的 - 你可以把它称为“临时”或在存储过程中包起来:

-- get the "latest" ID from the "Article" latest 
-- but you need to define *latest* by WHAT criteria?? A date?? The ID itself?? 
DECLARE @LastID INT 

SELECT TOP 1 @LastID = ID 
FROM dbo.Article 
ORDER BY .......... -- order by date? id? what?? 

-- get the detail data for that ID from the "Article_tbl" 
SELECT (list of columns) 
FROM dbo.Article_tbl 
WHERE ID = @LastID