I wanted to know a Query where in which i want to locate a specific table that contains two or more columns:
I tried:
SELECT *
FROM DB
WHERE TableName = 'TableName'
AND ColumName in('column1' , 'column2')
But this query will look if any of those columns are there, but i want it to return only if all of them are a match.
I hope this questions makes sense.
This should work for you in MySQL:
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME in ('column1','column2')
AND TABLE_SCHEMA='your_database'
GROUP BY table_name
HAVING COUNT(COLUMN_NAME) =2;
The operator IN is a concatenation of OR. However, there's no way to creare a short concatenation of AND as well.
See this question.
Related
I am working on project_name='Alpha' and dataset_name = 'Beta' and this dataset contains numerous tables. I want to get table details i.e, columns of a particular table i.e, table_name = 'Gamma'
To retrieve meta data information. you can use INFORMATION_SCHEMA.
Try with below query..
SELECT column_name FROM `project_name.dataset_name.INFORMATION_SCHEMA.COLUMNS`
WHERE table_name = <'Table Name'>;
For your solution, You can use INFORMATION_SCHEMA.COLUMNS
First, try with below:
SELECT * FROM Alpha.Beta.INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'Gamma' ;
Then you can select the details you want, only for the column name you can use below;
SELECT column_name FROM Alpha.Beta.INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'Gamma' ORDER BY ordinal_position asc ;
Please do let me know if it doesn't work out but flag it as an answer if it works.
EDIT: if it didn't work, can you please try putting Alpha.Beta.INFORMATION_SCHEMA.COLUMNS between escape character
How to select all tables that contain a specific column?
Is this what you expect?
demo: db<>fiddle
SELECT table_name
FROM information_schema.columns
WHERE column_name = 'your_column_name'
I have a table tblEmployeeInfowhich has atleast a 100+ column name.
I want to know how many column name are in that table. Is that possible?
NOTE:
tbleEmployeeInfo has no data inside yet.
I would recommend using the INFORMATION_SCHEMA views. You can see all the columns and their types by doing:
select c.*
from INFORMATION_SCHEMA.COLUMNS c
where table_name = 'tbleEmployeeInfo';
(You might want to include the table_schema as well.)
To get the count, just use COUNT(*):
select count(*)
from INFORMATION_SCHEMA.COLUMNS c
where table_name = 'tbleEmployeeInfo';
SELECT COUNT(*)
FROM sys.columns
WHERE object_id = object_id('tblEmployeeInfo')
I want to find the tables that contain both of the two columns together in one table. I tried this:
SELECT COLUMN_NAME, TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME in ('CurrencyName', 'CurrencyKey');
This query generates all the tables that has either one of the CurrencyName or CurrencyKey column.
But I want the table that has both of these columns together.
Please shoot some ideas.
Thanks!
You are close. You want to use group by and then validate that you have two matches using a having clause:
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME in ('CurrencyName', 'CurrencyKey')
GROUP BY TABLE_NAME
HAVING COUNT(*) = 2;
Note: To be sure you have the right table, you should use TABLE_SCHEMA as well in the query.
Can I write an sql query that can find the names of all tables in a database that have column name like %COL_FAX_ID%. If so, how?
Database used is oracle or db2.
For Oracle, you could try:
SELECT owner,
table_name,
column_name
FROM all_tab_cols
WHERE column_name LIKE '%COL_FAX_ID%'
ORDER BY owner,
table_name;
For a full list of the Oracle data dictionary views etc. see here.
Hope it helps...
I don't have an oracle install lying around to test this with but you should be able to do something like:
SELECT TABLE_NAME
FROM ALL_TAB_COLUMNS
WHERE COLUMN_NAME LIKE '%COL_FAX_ID%'
Dublicate of possible How to find all the tables in MySQL with specific column names in them?
Answer:
SELECT DISTINCT TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME IN ('columnA','ColumnB')
AND TABLE_SCHEMA='YourDatabase';
Edit:
Sorry I Miss saw the sql for beeing mysql tag...
But this may work aswell? Dunno. Gl & Hf
select distinct table_name from all_tab_columns where column_name like %'COL_FAX_ID%'
For oracle:
SELECT table_name,column_name from all_tab_columns
where column_name like '%COL_FAX_ID%'
For DB2, you will want to use the SYSCAT.COLUMNS catalog view.
SELECT *
FROM SYSCAT.COLUMNS
WHERE COLNAME LIKE '%COL_FAX_ID%'