MySQL statement search all tables for one specific column? - mysql

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'

Related

Is there any way to get list of tables by created on date.?

i found some answers but i found some answers here: Get the list of tables created on any date? but im getting, sys.tables doesn't exists.
i know here sys is database name.
If you want to find by a particular created date then:
SELECT * FROM information_schema.tables where TABLE_SCHEMA = 'YOUR_DATABASE_NAME'
AND CREATE_TIME = '2019-12-23 17:41:05'
Look into tables table in information_schema db. Query below:
SELECT table_schema,table_name,create_time
FROM information_schema.tables
WHERE table_schema=*your_database_name
AND table_name=*your_table_name;
Replace *your_database_name and *your_table_name accordingly.
Edit: sorry, your question is to lookup by created date.. here's the query:
SELECT table_schema,table_name,create_time
FROM information_schema.tables
WHERE DATE(create_time)="yyyy-mm-dd"
You can try it here : https://www.db-fiddle.com/f/n1yPjJKhyMLkWSP25htovn/0

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.

mysql | show tables with specified name

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_%'

MYSQL - List specific columns

I am trying to write a query which lists the names of the columns in an SQL table, however, I don't want all the columns - just specific ones. So, for example, if I was to put the COMMENT = 'test' for the columns which I want to list then I thought my query would be:
SHOW COLUMNS FROM `tbl_name` WHERE `COMMENT`='test'
This however throws an error.
Any ideas?
Thanks,
I think you can do this using information_schema.columns:
select column_name
from information_schema.columns c
where table_name = 'tbl_name' and
column_comment = 'test';
I think that SHOW COLUMNS can't have the WHERE clause, but you can try this:
SHOW COLUMNS FROM (SELECT * FROM `tbl_name` WHERE `COMMENT`='test')

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\_%';