var sidebar_align = 'right';
var content_container_margin = parseInt('290px');
var sidebar_width = parseInt('270px');
Unanswered: N records above/below the selected record.
Is there a way to fetch 'n' number of records above or below the selected record.
SELECT NAME FROM MyTable
WHERE NAME LIKE 'Andy'
Here the query output will be 'Andy'
So I require 'n' number of records to be displayed above or below 'Andy'
Last edited by andy982183; 04-02-10 at 09:23.
Originally Posted by andy982183
You want to look at adding an order by
clause and a limit
clause to your select statement.
This retrieves the results you want, although I am not sure how costly it gets as your table grow (in other words, as you collect more data in there).
SELECT M1.name, COUNT(*)
FROM MyTable M1 INNER JOIN MyTable M2
ON M1.name >= M2.name
GROUP BY M1.name
HAVING COUNT(*) <= (SELECT COUNT(*)
FROM MyTable M3
WHERE M3.name <= 'Andy');
Changing the predicate in the HAVING clause to >=, you will get records of Andy and below.
If there are records where the name column is NULL, they are not counted in the ordering.
Last edited by Ikviens; 04-02-10 at 12:46.