mysql | show tables with specified name - mysql

I have a tables in my database and would like to select specified tables by name:
abc_dshop_cats
abc_eshop_cats
abc_shop_cats
abc_shop_mods
abc_shopp_cats
How can I get these tables: abc_shop_cats and abc_shop_mods using the command below:
SHOW TABLES LIKE '%shop%'
And the command above is not working as return me all tables...
Thanks!

You could do like:
show tables like '%\_shop\_%'
This will get
abc_shop_cats
abc_shop_mods
but not
abc_dshop_cats
abc_eshop_cats
abc_shopp_cats

select table_name
from information_schema.tables
where table_name like '%shop%'

just use a normal SQL WHERE statement to do it.
SHOW TABLES WHERE Tables_in_mydbname LIKE '%_shop_%'

Related

write a query that applies to an entire db instead of a table

Is it possible to write a query that applies to an entire database as opposed to one table.
So instead of usin:
select * from table_name where columnName = ?
Can I say select * db_tables from from db where the table contains the column A?
Is is possible?
thanks
You sure can!
SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME like '%A'
You cannot do SELECT * FROM all the tables, but you can run a query aganist multiple tables using other statements in SQL.

NOT LIKE statement in Hive

In mysql I am using the following statement to find tables not like in a database.
show tables where `Tables_in_db` not like '%_table'
I am able to use the statement like below to find tables like in hive
show tables like '*table'
But unable to use the not like statement
show tables where `Tables_in_db` not like '*_table'
Is there an equivalent for this statement in Hive.
The below one can be used:
In SQL:
select * from tableName where columnName not like '%something%';
In Hive:
select * from tableName where not (columnName like '%something%');
Hope it helps.

MySql show tables query

I'm trying to select the tables names in my database using this query
show tables LIKE 'table1%'
it's working fine but at the same table I have a version of the table with some columns empty called 'table1_blank'. When I use the above statement I get every table starting with table1 including the "blank", how do I exclude 'table1_blank' from the selection?
Thanks.
You can use multiple like clauses using the where condition for SHOW TABLES:
SHOW TABLES
FROM `dbname`
WHERE `Tables_In_dbname` LIKE 'table1%'
AND `Tables_In_dbname` NOT LIKE 'table1\_%';
'_' has special meaning. 'any one character', so you need to change like this..
EDITED
SELECT TABLE_NAME
FROM information_schema.TABLES
WHERE TABLE_SCHEMA = 'your database name'
AND TABLE_NAME LIKE 'table1%'
AND TABLE_NAME NOT LIKE 'table1\_%';

MySQL statement search all tables for one specific column?

I am using the command line in MySQL on a Linux box.
I am logged in as root.
I need to find a field that in a table somewhere (in this schema there are 400+ tables)
Field is called 'discounts' or 'discounts'
I just need to find what table that specific field is in and if it is in more then one table with that feild or not.
Thanks for your help guys.
Edit
so the db is called magento and the tables are in there.
Try this:
SELECT * FROM INFORMATION_SCHEMA.COLUMNS
WHERE LOWER(COLUMN_NAME) = 'discount' or LOWER(COLUMN_NAME) = 'discounts';
Try this:
SELECT * FROM information_schema.columns WHERE column_name = 'discounts'

listing mysql tables starting with a particular letter

How to list all tables in MYSQL DB starting with "T"?
Assuming MySQL 5.0:
SHOW TABLES LIKE 'T%'
Here's the documentation
The true ANSI way:
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME LIKE 'T%'
Doesn't assume the presence or behaviour of the USE function. This should work in every DBMS supporting the ANSI INFORMATION_SCHEMA views, which definitely includes MySQL.
SHOW TABLES LIKE 'T%';
show tables from db_name like 't%'
I use this inside mysql shell program:
use information_schema;
select table_name from tables where table_name like 't%';