Last active
December 17, 2019 12:17
-
-
Save vielhuber/826c6f3b9ef670a0d3e54a1d2a96a06d to your computer and use it in GitHub Desktop.
mysql / postgresql: get total count 3 different alternatives with window function in one query #sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 2 queries (best) | |
SELECT col1, col2, col3 FROM bar ORDER BY col4 LIMIT 10; | |
SELECT COUNT(*) FROM bar; | |
# postgres | |
SELECT col1, col2, col3, count(*) OVER() AS full_count FROM bar ORDER BY col4 LIMIT 10; | |
# mysql (deprecated and slow!) | |
SELECT SQL_CALC_FOUND_ROWS col1, col2, col3 FROM bar ORDER BY col4 LIMIT 10; | |
SELECT FOUND_ROWS(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment