Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL select from a table where all columns equal to value

Tags:

sql

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.

like image 601
qaz Avatar asked Oct 29 '25 17:10

qaz


2 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.

like image 104
Gordon Linoff Avatar answered Oct 31 '25 09:10

Gordon Linoff


SELECT id, name, address, age 
FROM people 
WHERE 'value' in (id, name, address, age); 
like image 20
wviana Avatar answered Oct 31 '25 09:10

wviana



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!