Search code examples
databasems-accessselectselect-query

Select only some rows from database


I have the following query:

string Query = String.Format("SELECT ArticleName FROM tblArticles WHERE UserID={0} ORDER BY PostDate DESC", UserID);

I want to get not more than 3 different values for the 'ArticleName'.
Is it possible to do that? (without select all the 'ArticleName' just 3 of them?)

Thanks.


Solution

  • Typically you can use LIMIT 3 at the end of the query to achieve this

    string Query = String.Format(
        "SELECT ArticleName FROM tblArticleas WHERE UserID={0} ORDER BY PostDate DESC LIMIT 3",
        UserID
    );
    

    Or for Microsoft Access DB apparently you need to use TOP 3 at the start instead, eg:

    string Query = String.Format(
        "SELECT TOP 3 ArticleName FROM tblArticleas WHERE UserID={0} ORDER BY PostDate DESC",
        UserID
    );