64

Is SQL able to do something along the lines of this: SELECT * FROM table WHERE column = everything

8
  • 3
    Just remove that part of the string. If you don't care of column value - don't even put it to where
    – zerkms
    May 30, 2012 at 22:37
  • if you do not need a column then remove entire WHERE definition May 30, 2012 at 22:38
  • 20
    I am trying to solve this problem for convenience. I have 3 prepared statements that may or may not have 3 variables combined in the where clause. Rather than write several prepared statements for the combinations of all 3 variable values (null, not null), I'd like to ignore a variable in the where clause if it's not there.
    – Foo
    Jan 14, 2013 at 22:49
  • 3
    i've just run into this same issue as you. It seems silly from the outside (just drop the 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. May 27, 2016 at 15:51
  • 2
    OMG i am so stupid @zerkms nailed it. I was whole day strugling with what to put in part for WHERE to match anything like * and i just realized i jut need to remove it :D
    – Luka
    Feb 11, 2017 at 23:17

15 Answers 15

46

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.

2
  • 2
    My solution for a project I inherited, needed exactly this - Thank you so much Jan 12, 2019 at 10:46
  • 6
    This doesn't work for null values, which is fine for my case of a composite key.
    – Noumenon
    Jan 16, 2019 at 15:12
35
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?

3
  • 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
  • I like the 1=1 solution!
    – KinoP
    Sep 1, 2016 at 6:24
22

Your question allow three intrepretations:

  1. 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)
  2. You want the column to be set, bit you don't care to what: Use WHERE column IS NOT nULL
  3. You want a search, that can also display all records from a simple SQL template: SELECT * FROM table WHERE column LIKE '%$searchterm%'
9
  • 13
    I 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.
    – Truth
    Jun 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.
    – Truth
    Jun 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 when where a = x and b = anything would be useful if it was available.
    – Ivan
    Oct 14, 2019 at 20:21
11

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

1
  • 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.
    – Tanoro
    Mar 10, 2020 at 19:52
8

Everything or anything?

You could use a wildcard I guess.

SELECT * FROM table WHERE column LIKE "%"
6
  • 6
    Nope, you're wrong. = '%' means "the value exactly equal to one percent char"
    – zerkms
    May 30, 2012 at 22:38
  • 2
    And you're wrong once again, because it wouldn't include NULL values
    – zerkms
    May 30, 2012 at 22:39
  • 3
    What is the point of that? Then just SELECT * FROM table. If you don't need a filter, then don't use a WHERE clause.
    – nico
    May 30, 2012 at 22:39
  • (I wonder what the point of that is... although, perhaps for some brittle SQL generators...)
    – user166390
    May 30, 2012 at 22:39
  • 2
    This 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
    – cronoklee
    Sep 4, 2013 at 17:53
5
SELECT * FROM table WHERE column = IF (? = '', column, ?);
1
  • Thanks @Truth, will give this a go Jun 23, 2019 at 15:52
3

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 
2

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');
1

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 as where 1=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}}

0

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);
0

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."'

0

You can use

WHERE 1

1 is like in other language always true. So you have no filter

0

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

0

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;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.