如何从访问表中选择700个随机记录?

时间:2011-11-05 03:08:59

标签: c# ms-access

这段代码给我最后700条记录,而不是700条随机记录。

private string strsqlcommandBeta = " select top 700  LEFT(patronvalue17, INSTR(30, patronvalue17, ' ') -1) as patronvalue1,   Rnd([objectid]) AS Ran    from objectaer    " +
" order by objectdate desc" +
"  ";

2 个答案:

答案 0 :(得分:3)

我不在MS Access附近,但请试一试:

SELECT * FROM (
    SELECT top 700 LEFT(patronvalue17, INSTR(30, patronvalue17, ' ') -1) as patronvalue1, objectdate
    FROM objectaer
    ORDER BY Rnd([objectid]) ()
) order by objectdate desc

Rnd([objectid])应该给你一个随机数来排序,然后你按照你想要的那样排序。

答案 1 :(得分:0)

SELECT * FROM (
    SELECT top 700 LEFT(patronvalue17, INSTR(30, patronvalue17, ' ') -1) as patronvalue1, objectdate
    FROM objectaer
    ORDER BY newid()
) order by objectdate desc

newid()函数生成一个随机guid,可用于对表进行排序,在每次运行时给出不同的结果。我不知道你是否真的需要外部选择,它用于按日期降序对随机项目进行排序