简体   繁体   中英

get all the records count and first 100 records in hibernate and mysql

I am using server side pagination.For that initially i need to show first 100 records first and the no of buttons on the top.For example total i have 550 records i need to show 6 buttons at the top.When user clicks on second button i need to retrieve 101 to 199 records.

To get the total records i am using one database call and to get the first 100 records i am using another database call.

I am using hibernate ,so the query is like this

 select model from TableName model where ................

   FirstResult = 0
   MaxResult = 100

And to get the total no of records i am using

   select count(*) from TableName model where ................

Here the problem is i am running the similar query for 2 times. How i can combine these 2 queries into one?

Thanks in advance...

It's not the same query but conditions are very similar. There is nothing wrong to call 2 queries with common conditions.

SELECT *, (SELECT COUNT(*) FROM `TableName`) FROM `TableName`

works but there's 2 queries

(Server version: 5.5.32-MariaDB-1~precise-log - mariadb.org binary distribution)

you can use union in mysql http://dev.mysql.com/doc/refman/5.0/en/union.html

combine like this select model from TableName model where ................ UNION select count(*) from TableName model where ................

But you have to take care of separting the result of two queries

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM