Home > Enterprise >  Finding values using Where clause on corresponding columns ( MYSQL )
Finding values using Where clause on corresponding columns ( MYSQL )

Time:04-03

I trying to query this sql statement

SELECT * 
FROM `tablename` 
WHERE value1 IN (column1) 
AND value2 IN (column2) 
AND value3 IN (column3)...

So in this query the WHERE clause is finding the value1 in column1 and value2 in column2 and so forth

this although works perfectly...

but my problem is this, is there a better way to write this code something similar like The Insert query statement below.

INSERT INTO table_name (column1, column2, column3, ...) 
                VALUES (value1, value2, value3, ...);

u can see that the values correspond with the columns

could a similar statement be written like this

 SELECT * 
    FROM `tablename` 
    WHERE  VALUES (value1, value2, value3, ...); 
    IN (column1 AND column2 AND column3 AND ...) 

CodePudding user response:

I don't know why you even thought of the operator IN.

Your query should be written like:

SELECT * 
FROM `tablename` 
WHERE column1 = value1
  AND column2 = value2
  AND column3 = value3 ...

Another way to apply the same logic is this:

SELECT * 
FROM `tablename` 
WHERE (column1, column2, column3, ...) = (value1, value2, value3, ...)
  • Related