Skip to content

Instantly share code, notes, and snippets.

@jeffreyroberts
Last active December 23, 2015 05:49
Show Gist options
  • Save jeffreyroberts/6590046 to your computer and use it in GitHub Desktop.
Save jeffreyroberts/6590046 to your computer and use it in GitHub Desktop.
MySQL - Generate selects for counting rows of each table in a database
SELECT CONCAT(
'SELECT "',
table_name,
'" AS table_name, COUNT(*) AS exact_row_count FROM ',
table_schema,
'.',
table_name,
' UNION '
)
FROM INFORMATION_SCHEMA.TABLES
WHERE table_schema = '<database_name>'
@jeffreyroberts
Copy link
Author

Generates output like the following

| SELECT "asn_assignee" AS table_name, COUNT(*) AS exact_row_count F|
| SELECT "asn_assignees" AS table_name, COUNT(*) AS exact_row_count |
| SELECT "asn_assignment" AS table_name, COUNT(*) AS exact_row_count|
| SELECT "asn_assignment_entry" AS table_name, COUNT(*) AS exact_row|

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment