LoginSignup
3
1

More than 5 years have passed since last update.

MySQLを使ってページネーションする時はSQL_CALC_FOUND_ROWSを使うと良い

Last updated at Posted at 2018-03-05

備忘録のために記載

サンプル

  • 1. 一覧ページ用のSQLを投げる
SELECT SQL_CALC_FOUND_ROWS * FROM table LIMIT 0,100
  • 2. (1)のSQLで対象になった件数取得
SELECT FOUND_ROWS()

どうしてもUNIONしたい場合

SELECT SQL_CALC_FOUND_ROWS * FROM  table1 UNION ALL SELECT * FROM table2 LIMIT 0,100
3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1