473,385 Members | 1,888 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Sql Query to retrive Nth record?

Hi friends,

I m using MySql and i want to retrive N th recird from a table.
What is the query for that?
Plz help.

Thanx in advance.
Jul 10 '07 #1
6 21355
srinit
43
Hi friends,

I m using MySql and i want to retrive N th recird from a table.
What is the query for that?
Plz help.

Thanx in advance.
HI,
i am not clear with your requirement.this idea may help you.
You can select top nth record by selecting top(1) record based on your any one column in decreasing order. (or) select top(1) * from yourtable where row1 not in(select top(n-1)row1 from your table )
Jul 10 '07 #2
I dont know any query that directly gives you the Nth row. It you have a primary key, or a unique key filed, you can
select top n * from table where col not in (select top n-1 * from table)

Another method could be to select top n rows and order them in desc order and select top row from that
or
Select top n rows into a temporary table and select the last row from that table. this would work, even if your table does not have unique entries
Jul 10 '07 #3
Motoma
3,237 Expert 2GB
Hi friends,

I m using MySql and i want to retrive N th recird from a table.
What is the query for that?
Plz help.

Thanx in advance.
It seems you have posted in the wrong Forum, or you mislabeled your database as MySQL when you meant MSSQL.

If you are using MySQL, you can use a query like this:
SELECT * FROM table LIMIT 10,1
This will give you the 10th element in the query.

Perhaps, you could explain what you are trying to do, as typically this type of this isn't very useful.
Jul 10 '07 #4
Infide
28
In sql server you could perform the following trick.

SELECT TOP 1 Field1,Field2
FROM (
SELECT TOP 2 Field1,Field2
FROM table1
ORDER BY Field1 asc
) as data
order by Field1 desc

This will give you the 2nd record and only the 2nd record from table1.
Replace the 2 with in the sub select with any number.
Jul 10 '07 #5
Hi Vinay,

You can write the following query if you want to extract 2nd /3rd /4th record from the sql query ....

Say for example you are having 2 tables .....
1. EmpMaster (contains list of all employees)
2. EmpDept (contains information about employee department)

Now say for instance you need to fetch 2th record then you can use the following query .....

select top 1 qry.empid, qry.empfname, qry.emplname, qry.deptid, qry.deptname from
(
select top 2 em.empid, em.empfname,
em.emplname, ed.deptid, ed.deptname
from empmaster em, empdept ed
where em.empid = ed.empid order by ed.deptid ASC
) as qry
order by qry.deptid DESC

First the Inner query will be executed which will arrange the records in ASC order, then the control will go to outer query where the resultset will be reversed in DESC order. So this is how you retrieve the Nth record. If you need to fetch 3rd/4th/5th record then simply make the change in inner query TOP clause.

Cheers,
AMZY.
Mar 5 '10 #6
In MySql, To get the nth element:

SELECT * FROM <table_name>
where <condition>
limit <n-1>,1
Oct 7 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Bruno Alexandre | last post by:
Hi guys, withou using SP, I want to be able to add a Parameter to the SQL Query and retrive the Recordset so I can use the Paging property under the recorset object.... how can I do this? I'm...
0
by: Vijay | last post by:
Hello friends, I have a problem with one of the JOIN query here. Below is a brief description of the problem. tablename : test RecordId EffectiveDate othertableid value -------- ...
6
by: deko | last post by:
Can I reference a column other than the bound column of a ComboBox in a query? SELECT ... WHERE CtlID = Forms!frmMain!cbxCtlID.Column(1); (this does not seem to work) Or must I add a hidden...
2
by: Peter | last post by:
Hello! I have some records in a database (access), like this: 001111-10 001111-11 001111-12 002222-10 002222-11 002222-12
2
by: Gian Paolo | last post by:
Hi all on .net 2.0 i have a dataset and a table with some rows. Is there a way to retrive the current row index ? So i can retrive other value of that column... something like...
2
by: partha das | last post by:
sir i need a single sql query it may be sub query need to retrive the 5th highest salary from a employee table
1
by: asenthil | last post by:
Hai, Can any one tell me, How to retrive rows in alphabetcal order in the mysql database by using select query? thanks.. senthil
12
by: crs27 | last post by:
Hai All, i want to retrive records from a table between perticular datetime range. the query is select * from geo_trip_history where t.tr_start_date between '2008-02-02' and '2008-02-29' and...
11
by: sinner | last post by:
hi, i faced a problem when i tried to retrive value from two table using a common value. ie table1 has emp_id and name,table2 has emp_id and prod.. i tried to use the query select name,prod from...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.