Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to limit SHOW TABLES query

I have the following query:

SHOW TABLES LIKE '$prefix%'

It works exactly how I want it to, though I need pagination of the results. I tried:

SHOW TABLES LIKE '$prefix%' ORDER BY Comment ASC LIMIT 0, 6

I need it to return all the tables with a certain prefix and order them by their comment. I want to have pagination via the LIMIT with 6 results per page.

I'm clearly doing something very wrong. How can this be accomplished?

EDIT: I did look at this. It didn't work for me.

like image 286
Jaxkr Avatar asked Jul 24 '12 17:07

Jaxkr


People also ask

How do you set a limit in a query?

The limit keyword is used to limit the number of rows returned in a query result. “SELECT {fieldname(s) | *} FROM tableName(s)” is the SELECT statement containing the fields that we would like to return in our query. “[WHERE condition]” is optional but when supplied, can be used to specify a filter on the result set.

How do you count limits in SQL?

The SQL SELECT LIMIT statement is used to retrieve records from one or more tables in a database and limit the number of records returned based on a limit value. TIP: SELECT LIMIT is not supported in all SQL databases. For databases such as SQL Server or MSAccess, use the SELECT TOP statement to limit your results.


2 Answers

The above cannot be done via MySQL Syntax directly. MySQL does not support the LIMIT clause on certain SHOW statements. This is one of them. MySQL Reference Doc.

The below will work if your MySQL user has access to the INFORMATION_SCHEMA database.

SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'DATABASE_TO SEARCH_HERE' AND TABLE_NAME LIKE "table_here%"  LIMIT 0,5;
like image 104
Mike Mackintosh Avatar answered Sep 28 '22 02:09

Mike Mackintosh


Just select via a standard query instead of using SHOW TABLES. For example

select table_name from information_schema.tables

Then you can use things like ASC and LIMIT, etc...

like image 21
Jer In Chicago Avatar answered Sep 28 '22 02:09

Jer In Chicago