qaz
qaz

Reputation: 33

SQL select from a table where all columns equal to value

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.

Upvotes: 3

Views: 6655

Answers (3)

wviana
wviana

Reputation: 1937

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

Upvotes: 1

Zac
Zac

Reputation: 2081

SELECT id, name, address, age 
FROM people 
WHERE id = 'value' 
AND name = 'value' 
AND age = 'value' 
AND address = 'value';

Upvotes: 0

Gordon Linoff
Gordon Linoff

Reputation: 1269873

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.

Upvotes: 2

Related Questions