To pull count of a specific column of 10k tables in MySQL - mysql

SELECT Count(COLUMN_NAME)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'TableName' AND TABLE_SCHEMA = 'DatabaseName'
I tried this to pull count from a single table.
How to achieve this with multiple tables which is more than 10k

Remove your filter condition of Table name AS below
SELECT Count(COLUMN_NAME)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'DatabaseName'

Related

MySQL Database last inserted row in all tables

I have staging database and needs to find last inserted row in all database tables, some tables also dont have timestamp column or Ids in orderwise. if there is any way please...
Use this
SELECT
COLUMN_NAME,
ORDINAL_POSITION
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = 'YOUR_DATABASE_NAME'
AND TABLE_NAME ='YOUR_TABLE_NAME'
ORDER BY ORDINAL_POSITION DESC
LIMIT 1;
Information_schema.columns stores column specific information
You can get all table and its last inserted id
SELECT
TABLE_NAME,
max(ORDINAL_POSITION) last_inserted_id
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = 'YOUR_DB_NAME'
GROUP BY
TABLE_NAME

Mysql statement to list tables of certain columns?

I'm trying to figure out the foreign keys relationship within my MySQL databases.
I have many tables that has the "mydomain_id" column located in other tables.
Is there a command line I can use to list all tables that has that particular column?
SELECT
DISTINCT `table_name`
FROM
INFORMATION_SCHEMA.COLUMNS
WHERE
COLUMN_NAME IN ('name') AND
TABLE_SCHEMA='symfony_backend';
Gives: (all the tables listed below exist in my database symfony_backend)
table_name
'child'
'country'
'league'
'parent'
'player'
'schools'
'student'
'team'
'user'
You can feed more column names with: COLUMN_NAME IN ('name', 'another_column', 'and_another_one')
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_schema = 'db_name'
AND column_name = 'mydomain_id'

Get the different column names on two tables MySQL

I have two tables with almost identical column names :
Here's a sample table
I want to get the columns names highlighted in red.
Any idea?
Try this:
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'your_schema' AND
TABLE_NAME = 'table2' AND
COLUMN_NAME NOT IN (
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'your_schema' AND
TABLE_NAME = 'table1');
The above query returns the column names of table2 not present in table1.
Demo here

How to identify a column with name exist in all tables?

i want to query a database which has huge tables for the columns with specific name
ex: fetch all the tables which has column name like 'name'
Thanks.
Try
SELECT table_name -- you might want to add DISTINCT if you use pattern matching with LIKE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_schema = SCHEMA()
AND column_name = 'name' -- or column_name LIKE '%name%'
Here is SQLFiddle demo

find columns in all database

If I have a database some_database with multiples tables like:
table_one
table_two
table_three
where
table_one has CPV_one
table_two has CPV_two
table_three has CPV_three
I need to find all tables that have a column like '%CPV%'
This can be done by sql query? I want to avoid check all tables one by one.
You can query information_schema to get a list of each table in your database with a matching column name:
SELECT
TABLE_NAME, COLUMN_NAME
FROM
information_schema.COLUMNS
WHERE
TABLE_SCHEMA = 'SOME_DATABASE'
AND COLUMN_NAME LIKE '%CPV%'
EDIT (selecting only the TABLE_NAME column)
As pointed out in a comment, if you want to select only the name of the table (without the list of columns that match), you should also use the DISTINCT keyword - otherwise a duplicate row will be returned for the same table for each column that matches:
SELECT
DISTINCT TABLE_NAME
FROM
information_schema.COLUMNS
WHERE
TABLE_SCHEMA = 'SOME_DATABASE'
AND COLUMN_NAME LIKE '%CPV%'
SELECT DISTINCT TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME LIKE '%CPV%'
AND TABLE_SCHEMA='YourDatabase';
How about querying the MySQL information_schema?
SELECT TABLE_NAME
FROM information_schema.COLUMNS
WHERE COLUMN_NAME LIKE '%CPV%'
Here you have the code to get the table names with the needed condition:
SELECT DISTINCT TABLE_NAME
FROM information_schema.Columns
WHERE COLUMN_NAME LIKE '%CPV%';
For more details on the INFORMATION SCHEMA take a look here
And for COLUMNS table here