how can a specify header is in which table? - mysql

suppose i have three table with names : broker,man and coach.
i want to know that for example 'name' field
is in which table.
how can realize it?

Strange question indeed.
select column_name,table_name
from information_schema.columns
where table_schema = 'your_db' and table_name in ('broker','man','coach') and column_name = 'name'

Related

How do I find a table in MySQL with 2 specific columns in it

Similar to How to find all the tables in MySQL with specific column names in them? I would like to find the table with 2 specific columns, not either or.
I've tried combining with AND but no dice.
For instance, I want to search the database for the specific tables that contain both CategoryID and LotNumber columns.
Through the information_schema.columns table, grouping the matching columns by table and returning only those with number equal to 2:
SELECT table_name
FROM information_schema.columns
WHERE (column_name = 'colname1' OR column_name = 'colname2')
[AND table_schema = 'dbname']
GROUP BY table_name
HAVING count(*) = 2;
You can try something like :
SELECT * from TableName where obj1 = "obj1" and obj2= "obj2"
this is an example.
Let me know how it worked :)

Search columns in mysql table?

I have a table with 300+ column. Looking for a specific column is like nightmare. Is there any query where If I would like to search for columns stats with 'grand' can be listed...
You can use show columns from table with where. Try the following query,
SHOW COLUMNS FROM tablename WHERE field like 'grand%';
Just put in your tablename after FROM and it would work.
select * from myTable where mycolumn like 'grand%'
Try this.
SELECT
table_name,
column_name,
data_type,
ordinal_position
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_schema = 'DatabaseName' --- the database you want to search
AND table_name = 'yourTableName'
AND column_name LIKE '%Grand' ;
This one worked for me. Its shows all columns and table in entire database
SELECT TABLE_NAME, COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE column_name OR table_name LIKE '%sale%';

How to get column names with where condition included?

I am trying to use :
SHOW COLUMNS FROM #__tablename WHERE Field.columnname = 'any value' ;
If I use it without where condition then it retrieves all column names but I want to fetch specified column name.
Please suggest.
You can use information_schema.columns table instead of SHOW COLUMNS statement. The information_schema.columns table is the same as SHOW COLUMNS, but you can work with result-set as with ordinary table. For example -
SELECT * FROM information_schema.columns
WHERE table_schema = 'table name' AND table_name = 'table name';
Specify columns you need and WHERE filter.
You Can use information_schema.columns , and add a filter to fetch column you want, I think it works
SELECT * FROM information_schema.columns
WHERE table_schema = 'table name'
AND table_name = 'table name'
AND column_name = 'Column name'

MySQL query to select table names by column names

I'm trying to select all tables from a specified database that contain columns 'lang' and 'project'. This is what i'm doing:
SELECT DISTINCT(ISC1.TABLE_NAME) AS `table` FROM INFORMATION_SCHEMA.COLUMNS AS ISC1
JOIN INFORMATION_SCHEMA.COLUMNS AS ISC2 ON
(ISC1.TABLE_SCHEMA=ISC2.TABLE_SCHEMA AND ISC1.TABLE_NAME=ISC2.TABLE_NAME AND ISC2.COLUMN_NAME='project')
WHERE ISC1.COLUMN_NAME='lang' AND ISC2.COLUMN_NAME='project' AND ISC1.TABLE_SCHEMA='some_database'
It does work, but i have a feeling it is a bad way to write this kind of query. If someone can improve on it, that would be nice.
Now i have to change this query to select all tables that have 'lang' column, but do NOT have 'project' column. And honestly i can't figure out where to start...
Thanks for your help
SELECT DISTINCT(ISC1.TABLE_NAME) AS `table`
FROM INFORMATION_SCHEMA.COLUMNS AS ISC1,
INFORMATION_SCHEMA.COLUMNS AS ISC2
WHERE ISC1.TABLE_SCHEMA=ISC2.TABLE_SCHEMA
AND ISC1.TABLE_NAME=ISC2.TABLE_NAME
AND ISC2.COLUMN_NAME='project'
AND ISC1.COLUMN_NAME='lang'
AND ISC1.TABLE_SCHEMA='some_database'
Without project column :
SELECT DISTINCT(ISC.TABLE_NAME) AS `table`
FROM INFORMATION_SCHEMA.COLUMNS AS ISC
WHERE ISC.COLUMN_NAME='lang'
AND ISC.TABLE_SCHEMA='some_database'
AND NOT EXISTS(SELECT *
FROM INFORMATION_SCHEMA.COLUMNS C
WHERE C.COLUMN_NAME = 'project'
AND C.TABLE_NAME=ISC.TABLE_NAME
AND C.TABLE_SCHEMA=ISC.TABLE_SCHEMA)

MySQL, search a database and tell what tables contain two specific columns?

I have a database with well over 90 tables and I am trying to figure which, if any, of them have the same two specific columns. The code I am looking for would be something like this:
SHOW TABLES IN `database`
WHERE column = 'columnA'
AND column = 'columnB';`
Is this even possible?
This will give you all tables having either of the two columns, which you can browse through to find what you need.
select *
from information_schema.columns
where column_name in ('columnA', 'columnB')
order by table_name, column_Name
SELECT TABLE_SCHEMA AS `schema`, TABLE_NAME AS `table`, COLUMN_NAME AS `column`
FROM `information_schema`.`COLUMNS`
WHERE COLUMN_NAME IN('column1','column2')