SHOW TABLES statement with multiple LIKE values - mysql

mysql> SHOW TABLES like 'cms';
+-------------------------+
| Tables_in_tianyan (cms) |
+-------------------------+
| cms |
+-------------------------+
1 row in set (0.00 sec)
Result
mysql> SHOW TABLES like 'cms' or like 'role';
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual...
How can I filter by multiple conditions ?

You need to use the WHERE clause. As shown in the docs, you can only have a single pattern if you use "SHOW TABLES LIKE ...", but you can use an expression in the WHERE clause if you use "SHOW TABLES WHERE ...". Since you want an expression, you need to use the WHERE clause.
SHOW TABLES
FROM `<yourdbname>`
WHERE
`Tables_in_<yourdbname>` LIKE '%cms%'
OR `Tables_in_<yourdbname>` LIKE '%role%';

You can just use a normal SQL WHERE statement to do it.
SHOW TABLES WHERE Tables_in_tianyan LIKE '%cms%'

show tables from mydb
where
Tables_in_mydb like '%statistics%'
or Tables_in_mydb like '%device%';

You take table list using the below code
select TABLE_NAME from information_schema.TABLES where TABLE_SCHEMA = 'database_name'
Hope it will help you.

this will help
SELECT
TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME
LIKE 'cms%';

Related

Exclude tables in show tables like statement in MySQL

In MySQL database I have some tables which end with _history.
Now I want to select tables that are like _history.
I have done like below.
show tables like '%_history`
Now I got the desired result.
Now in this result I got some tables which start with temp. Ex: temp_102_history.
Is there a way to exclude the tables that start with temp in the show tables like '%_history statement.
Select table_name
from information_schema.tables
Where table_name like '%_history'
and table_name not like 'temp%'
and table_schema='your database'
You can use information_schema database for this.
Try this
SHOW TABLES
WHERE tables_in_test NOT LIKE 'temp_%'
AND tables_in_test LIKE '%_history'
Replace test with your DB name.

how to select all tables with certain postfix in mysql?

I'm going to select all tables from a database that their names ends with "_language".
For example : "product_language" or "category_language" .
Can you help me?
You can select all tables using information schema using bellow given query
SELECT
TABLE_NAME
from
information_schema.TABLES
where
TABLE_SCHEMA = 'Your_schema_name'
and TABLE_NAME LIKE '%_language';
Reference for MySQL SHOW command
You can simnply go for SHOW command, like
SHOW tables like '%_language';
Then it will display all the tables in MySQL DB with the table names that are ended by the name '_language'
mysql> SHOW TABLES LIKE '%_language';
OUTPUT as
+---------------------+
| Tables |
+---------------------+
| product_language |
| category_language |
+---------------------+

Alias to Show Tables MySQL Result

When executing SHOW TABLES FROM your_db_name_here inside MySQL, the result has Tables_in_{your_db_name_here} as its indexes. I'd like to change that, like this:
SHOW TABLES as _tables FROM mydb;
or something like
SELECT Tables_in_mydb AS _tables FROM (SHOW TABLES FROM mydb);
Any ideas?
It's usually easier to interact with the INFORMATION_SCHEMA database:
http://dev.mysql.com/doc/refman/5.0/en/tables-table.html
Example:
SELECT TABLE_NAME AS _tables
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'mydb'

Show table status in a subquery?

Is this not supposed to work in MySQL?
select * from (show table status like '%fubar%') as t1;
or even
select name, rows from (show table status like '%fubar%') as t1 where rows>0;
This is the error I'm receiving:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'show table status like '%fubar%') as t1' at line 1
Can show table foo like '%something%' or show tables like '%something%' not be used in a subquery in this way? How else could you select from all tables matching a certain pattern?
SELECT table_name as name, table_rows as rows FROM information_schema.tables as t1
WHERE table_rows > 0
Here is an alternate way of retrieving the information you are looking for.

select privilege on more than one table

How do I grant select privilege on more than one table in a single statement?
mysql> grant select on dbName.crw_changes to sink;
Query OK, 0 rows affected (0.02 sec)
mysql> grant select on dbName.crw_changes, dbName.bs_services to sink;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' dbName.bs_services at line 1
You can't do it in standard SQL. More than one user is permitted, but only one table.
That's generally true for most SQL dialects, too. There are reasons for the limitation, most notably related to checking permissions on the separate tables, though they were perhaps more relevant in the 1980s than they are now.
You can't. GRANT SELECT only works on one table at a time.
There's a workaround, however. You can do something like:
SELECT CONCAT('GRANT SELECT ON test.', TABLE_NAME, ' to ''foouser'';')
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'test'
AND TABLE_NAME LIKE 'foo_%'
The above extracts tables in the 'test' schema that start with 'foo_' and prints them out. You can then take those names and copy and paste them into a stored procedure or query builder window and execute them.
By the way, the code is untested (by me). I took it from http://lists.mysql.com/mysql/202610.