SQL select from a table where all columns equal to value

Viewed 2565

Is there a way to select everything in a database table where every column is equal to a value?

Suppose I have this table:

+-----------------------------+
|           people            |
+-----------------------------+
|  id  | name |address|  age  |
+------+------+-------+-------+
|1     |Arnold|  USA  |   31  |
|2     |Andeng|  PHI  |   18  |
|3     |  Bea |  UK   |   52  |
+------+------+-------+-------+

and the SQL statement would be like:

SELECT id, name, address, age 
FROM people 
WHERE id, name, age, address LIKE '%$value%'

I do hope you get what I mean.

3 Answers

In SQL, you need to test each value independently:

SELECT id, name, address, age
FROM people
WHERE id LIKE '%$value%' AND
      name LIKE '%$value%' AND
      age LIKE '%$value%' AND
      address LIKE '%$value%';

First note. This assumes that the columns are strings. LIKE should only be used on strings (explicitly convert other types if necessary).

Second note. If you are passing parameters into a query, use parameters. Don't munge the query string.

SELECT id, name, address, age 
FROM people 
WHERE id = 'value' 
AND name = 'value' 
AND age = 'value' 
AND address = 'value';
SELECT id, name, address, age 
FROM people 
WHERE 'value' in (id, name, address, age); 
Related