2011-11-05 81 views
-2

這段代碼給了我最後的700條記錄,而不是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" + 
" "; 

回答

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

訪問,而不是sql server –

+0

@JoelCoehoorntouché。我已經更新了它。 – Daryl

+0

現在我已經upvoted它;) –

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。我不知道你是否真的需要外部選擇,它是用來按日期排序隨機項目的降序

+0

你做了我在我的第一個答案,這是JoelCoehoorn評論的同一個錯誤。這是針對Access的,而不是SQL SERVER – Daryl