Home > Enterprise >  SQL Match table and multiple columns
SQL Match table and multiple columns

Time:02-14

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.

CodePudding user response:

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;

CodePudding user response:

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.

  •  Tags:  
  • sql
  • Related