Home > Net >  SQL 'WHERE' retrieve all data from a filter
SQL 'WHERE' retrieve all data from a filter

Time:10-03

With this query:

SELECT * 
FROM table1 
WHERE name = 'Peter'

I can retrieve all data from Peter from table1. This can be done with the "Wildcard *".

Question

Is there any kind of wildcard for the WHERE part? For example:

SELECT * 
FROM table1 
WHERE name = *

This option of course not working, but I am looking for a wild card there so that all names will be included in my query. I know it's easier to remove the WHERE statement, but due to some reasons I still need it.

CodePudding user response:

SELECT * 
FROM table1 
WHERE True OR name = 'Peter'
;

This may look silly, but it can come in handy when generating query strings, eg in PHP/PDO:


$query = "SELECT * FROM names
     WHERE $ignore_name OR name = :the_name
     AND $ignore_address OR address LIKE(:the_address)";

, where the $ignore_xxx varables are either True or False, and completely under your control (not user-input!)

CodePudding user response:

select * 
from table1 
where name = 'Peter' or name = name;

CodePudding user response:

You can query output into your WHERE clause like so:

SELECT *
FROM table1
WHERE [name] IN (SELECT DISTINCT [name]
                    FROM table1)
  • Related