1

The problem is same as: Search mysql table starting from last row

╔════╦════════════╦════════════╦═════════════╦════════════╗
║ id ║  field_1   ║   field_2  ║   field_n   ║    time    ║
╠════╬════════════╬════════════╬═════════════╬════════════╣
║  1 ║ data_field ║ data_field ║ data_field  ║ 1369748934 ║
║  2 ║ data_field ║ data_field ║ data_field  ║ 1369748935 ║
║  3 ║ data_field ║ data_field ║ data_field  ║ 1369748936 ║
║  4 ║ data_field ║ data_field ║ data_field  ║ 1369748936 ║
║  5 ║ data_field ║ data_field ║ data_field  ║ 1369748938 ║
║  6 ║ data_field ║ data_field ║ data_field  ║ 1369748939 ║
╚════╩════════════╩════════════╩═════════════╩════════════╝

I want to retrieve first 'n' results starting at row index 'i' meeting certain criteria:

Case 1) Search should start from index 'i' and then move downwards

Case 2) Search should start from index 'i' and then move upwards

Case 1) is pretty straight. How can case 2) be achieved ?

Community
  • 1
  • 1
Anmol Gupta
  • 2,797
  • 9
  • 27
  • 43
  • 2
    are you after pagination? http://stackoverflow.com/questions/16088949/jpa-query-to-select-based-on-criteria-alongwith-pagination – xQbert Jul 24 '15 at 16:04
  • @xQbert Exactly, but not getting how to write query/criteria query such that it starts going upwards after starting for the case 2) – Anmol Gupta Jul 24 '15 at 16:09
  • For the case 1) I am using the criteria query the same way as mentioned in the above link. – Anmol Gupta Jul 24 '15 at 16:12

2 Answers2

0

Case 1- Use OFF SET to get data from particular position.

Case 2- While for this you can start from 0 index till the ith index with LIMIT OR you can also use off set to start from particular index till the limit index.

Here are some links which may help you on LIMIT and OFF SET :

http://www.petefreitag.com/item/451.cfm

http://www.w3schools.com/php/php_mysql_select_limit.asp

Abhishek Ginani
  • 4,511
  • 4
  • 23
  • 35
0

To change direction - try orderBy(...) In your case, you would need to order by 'id' with Asc or Desc values

AntonZ
  • 136
  • 5