Is SQL able to do something along the lines of this: SELECT * FROM table WHERE column = everything
15 Answers
For anyone who NEEDS the column name in the query for whatever reason (probably dynamic SQL), a nice alternative would be SELECT * FROM table WHERE column = column
This is very similar to WHERE 1=1
, however it includes the column name, which my solution required, and maybe a few others will require as well.
-
2My solution for a project I inherited, needed exactly this - Thank you so much Jan 12, 2019 at 10:46
-
6This doesn't work for null values, which is fine for my case of a composite key.– NoumenonJan 16, 2019 at 15:12
SELECT * FROM table
If you're generating SQL dynamically, it's
SELECT * FROM table WHERE 1=1
The 1=1
placeholder allows you to return all records, or substitute an actual condition if you're returning a subset or need additional conditional statements.
See Also
Why would someone use WHERE 1=1 AND <conditions> in a SQL clause?
-
But if it's a prepared statement, 1=1 will not work as it is automatically escaped, no ? Apr 26, 2016 at 14:31
-
If it is a prepared statement, doesn't it already have a WHERE? Apr 26, 2016 at 14:32
-
Your question allow three intrepretations:
- You don't care about the column: drop the it from the where clause (or drop the where clause alltogether, if this was the only subclause)
- You want the column to be set, bit you don't care to what: Use
WHERE column IS NOT nULL
- You want a search, that can also display all records from a simple SQL template:
SELECT * FROM table WHERE column LIKE '%$searchterm%'
-
13I have this problem and I can't see the answer here. My interpretation is this - I have a variable which I use to define the value of a column. BUT if that variable is 'all', I am not looking for the value 'all', rather the query act like the WHERE clause is not there. I do not want to necessarily write two queries. I am looking for a symbol, eg. '*' which looks for everything. Jul 23, 2018 at 11:46
-
1@gavin - Did you ever find a solution? I'm still looking for one. Frustrating that a wrong answer was accepted here.– TruthJun 21, 2019 at 19:55
-
2@Truth - no, in the end I wrote two queries -
if (var=all){query without WHERE}else {query with WHERE = var}
Jun 22, 2019 at 8:29 -
1@gavin - Ha! I came up with a very similar solution before you wrote back. See below.– TruthJun 23, 2019 at 11:31
-
3
You don't care about the column: drop the it from the where clause (or drop the where clause alltogether, if this was the only subclause)
- some times you just use a prepared statement which has a parameter in the where clause and you don't want to limit the result to particular value of the parameter in a particular case. That's whenwhere a = x and b = anything
would be useful if it was available.– IvanOct 14, 2019 at 20:21
You can use column name itself in your query:
SELECT * FROM TABLE WHERE COLUMN = COLUMN
for example:
SELECT * FROM Student WHERE YEAR = YEAR
or by using a Auxiliary parameter:
SELECT * FROM Student WHERE YEAR = (CASE WHEN @year IS NOT NULL THEN @year ELSE YEAR END)
so you can hold on "=" char
-
I like your first example the most. I have an object method that watches for user inputs. When the input is empty, it simply excludes the clause. This means the query is highly variant and difficult to test with. A wildcard match (or making it match itself) works, but I'm concerned that the reason MySQL doesn't do this is because of performance loss.– TanoroMar 10, 2020 at 19:52
Everything or anything?
You could use a wildcard I guess.
SELECT * FROM table WHERE column LIKE "%"
-
6Nope, you're wrong.
= '%'
means "the value exactly equal to one percent char"– zerkmsMay 30, 2012 at 22:38 -
2
-
3What is the point of that? Then just
SELECT * FROM table
. If you don't need a filter, then don't use aWHERE
clause.– nicoMay 30, 2012 at 22:39 -
(I wonder what the point of that is... although, perhaps for some brittle SQL generators...)– user166390May 30, 2012 at 22:39
-
2This is very useful! For those asking why, consider:
SELECT * FROM table WHERE field='22' OR field LIKE '%';
This would return a fallback row if the desired row was not found Sep 4, 2013 at 17:53
SELECT * FROM table WHERE column = IF (? = '', column, ?);
This is late but may be helpful to others
You can try this.
where
isnull([column], '') = CASE WHEN @column IS NULL THEN isnull([column], '') ELSE @column END
Are you perhaps looking for the IN criteria operator?
SELECT * from table where column in (1,2,3,4,5) or column in ('value', 'value2', 'value3');
Well I had this same issue too and the following solved my problem:
... where column = case when @variable = 'all' then column else @variable end
Keep it in mind that you must always send a default value , i set my default value as 'all'. So if i set
@variable = 'all'
, mysql reads it as :where column = column
which is the same thing aswhere 1=1
I've faced this problem while developing dynamically composing query. Here is my solution in short:
WHERE (column = ANY (SELECT distinct column) OR column IS NULL)
This works with NULL values and practically it is identical to empty WHERE statement. I use brackets in order to keep the ability to add more WHERE options using AND operator.
It means that this:
select count("objectId"), "source"
from "SomeTable"
where "createdAt" > '2020-07-06'
and ("source" = any (select distinct "source") or "source" is null)
and ("country" = any (select distinct "country") or "country" is null)
and "channel" is not null
group by "source"
equals to this:
select count("objectId"), "source"
from "SomeTable"
where "createdAt" > '2020-07-06'
and "channel" is not null
group by "source"
So I can make a query template:
...
WHERE (column = {{filter_value}} )
...
and set ANY (SELECT distinct column) OR column IS NULL)
as default value for {{filter_value}}
If this helps anyone... just to point out, if you have a problem such as SELECT * FROM something WHERE (can be specific or everything) such as filtering stuff,
you can use
SELECT * FROM something as s WHERE (?1 = 0 OR ?1 = s.type = ?1)
0 here is a just predefined for all, so feel free to change, i needed this while using JPA repositories with hibernate for filtering. You can't do this medicinally as suggested by a previous answer because of the safety with prepared statements. Where the ?1 corresponds to :
Page<Something> filterSometing(Long type,Pageable pageable);
put elvis operator to that field
" . ($variable == 'selected value' ? '' : "AND column='" . $variable . "' ") . "
so where u select all in the options in the html page this ^^ field will not run but when u select something this code will show up like column='".$variable."'
You can use
WHERE 1
1 is like in other language always true. So you have no filter
FOR PROCEURES: if you send a parameter for the condition, you can define a constant value to get all rows For example you can say when i send %
for it get all rows
SELECT * FROM allstocks WHERE allstocks.storestockid=key1 OR '%'=key1
you can replace '%'
with 1
or anything you want
If you want to do this, you basically need to use WHERE true
. Since MySQL doesn't accept true though, you have to substitute it for a number.
As integers, 0 is equal to false and 1 is equal to true.
This means, if you want to achieve "WHERE everything", you can simply use:
WHERE 1
You can also use something that always outputs as true, such as:
WHERE 1=1
or
WHERE 1+2-1=2
You can also just remove the WHERE declaration in general:
SELECT * FROM table;
instead of
SELECT * FROM table
WHERE 1=1;
column
value - don't even put it towhere
where
clause) but it would make it easier with only needing to write one parameterized query, rather than a bunch of conditions. curious if anything has changed since 2012.