Click here to Skip to main content
15,065,914 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
dear all

i want to show top 3 record
i have a table where 2 field ID and cat_type
i want to show top 3 record from cat_type column

SQL
select top 3 ID , cat_type from catagory

i used this query but this query show all the record from cat_type column

how can i do pls help me
Posted

Try
SQL
SELECT TOP 3 cat_type FROM catagory
   
Comments
um_dw 26-Jun-11 1:39am
   
dear sir this query show all record
my database in ms-access pls help me
Dr.Walt Fair, PE 26-Jun-11 1:51am
   
What does your data look like? How many records do you have?
if your database is MSSQL try this

Select Top 3 ID, cat_type from category

or

if your database is MYSQL try this

SELECT ID,cat_type FROM category LIMIT 3

if not working please let me know

Thanks
   
Comments
um_dw 26-Jun-11 1:17am
   
dear Sir my database on ms-access
i want show only 3 top record from cat_type column
so tell me what is the query i write
mahabubur rahman 26-Jun-11 4:27am
   
MS Access and MSSQL server both are same qry
------
Select Top 3 ID, cat_type from category
Use TOP with ORDER BY clause in your query

If you want display TOP 3 records based on ID by descending order,
SQL
SELECT TOP 3 ID, cat_type FROM catagory ORDER BY ID DESC

or if you want display TOP 3 records based on ID by ascending order,
SQL
SELECT TOP 3 ID, cat_type FROM catagory ORDER BY ID ASC


If you want display TOP 3 records based on cat_type by descending order,
SQL
SELECT TOP 3 ID, cat_type FROM catagory ORDER BY cat_type DESC

or if you want display TOP 3 records based on cat_type by ascending order,
SQL
SELECT TOP 3 ID, cat_type FROM catagory ORDER BY cat_type ASC


Use the query based on your requirement.

EDIT
---------------------------------------------------------------
If you want to display only cat_type column then try like below
SQL
SELECT TOP 3 cat_type FROM catagory ORDER BY cat_type DESC

If still you are not clear then please show us what output do you want? Include the table structure with sample data. And show sample output which do you want. Let me know.
   
v2
Comments
um_dw 26-Jun-11 1:15am
   
dear Sir
every query show all record from the table
i want to show only 3 top record from cat_type column
and my database in ms-access
thatraja 26-Jun-11 1:33am
   
Check my updated answer
um_dw 26-Jun-11 1:25am
   
this is my code pls check its
OleDbConnection myConnection = default(OleDbConnection);
OleDbDataAdapter myCommand = default(OleDbDataAdapter);
// Create a connection to the "pubs" SQL database located on the
// local computer.
myConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath(@"App_Data/funda.mdb"));
// Connect to the SQL database using a SQL SELECT query to get all
// the data from the "Titles" table.

myCommand = new OleDbDataAdapter("SELECT TOP 3 ID, cat_type FROM catagory ORDER BY ID DESC", myConnection);
// Create and fill a DataSet.
DataSet ds = new DataSet();
myCommand.Fill(ds);
// Bind MyDataList to the DataSet. MyDataList is the ID for
// the DataList control in the HTML section of the page.
//this.MyDataList.DataSource = ds;
this.MyDataList.DataBind();
my code is pls read every body and give solution

OleDbConnection myConnection = default(OleDbConnection);
OleDbDataAdapter myCommand = default(OleDbDataAdapter);
// Create a connection to the "pubs" SQL database located on the
// local computer.
myConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath(@"App_Data/funda.mdb"));
// Connect to the SQL database using a SQL SELECT query to get all
// the data from the "Titles" table.
myCommand = new OleDbDataAdapter("SELECT TOP 3 ID, cat_type FROM catagory ORDER BY ID DESC", myConnection);
// Create and fill a DataSet.
DataSet ds = new DataSet();
myCommand.Fill(ds);
// Bind MyDataList to the DataSet. MyDataList is the ID for
// the DataList control in the HTML section of the page.
//this.MyDataList.DataSource = ds;
this.MyDataList.DataBind();
   
check my code
OleDbConnection myConnection = default(OleDbConnection);
OleDbDataAdapter myCommand = default(OleDbDataAdapter);
// Create a connection to the "pubs" SQL database located on the
// local computer.
myConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath(@"App_Data/funda.mdb"));
// Connect to the SQL database using a SQL SELECT query to get all
// the data from the "Titles" table.
myCommand = new OleDbDataAdapter("SELECT TOP 3 ID, cat_type FROM catagory ORDER BY ID DESC", myConnection);
// Create and fill a DataSet.
DataSet ds = new DataSet();
myCommand.Fill(ds);
// Bind MyDataList to the DataSet. MyDataList is the ID for
// the DataList control in the HTML section of the page.
//this.MyDataList.DataSource = ds;
this.MyDataList.DataBind();
   
you are using ms access

Try this out..

SELECT TOP 3 cat_type FROM tblname

it will show top 3 records from your table.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900