66

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

Steve Robbins
  • 13,672
  • 12
  • 76
  • 124
Majo0od
  • 2,278
  • 10
  • 33
  • 58
  • 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 '12 at 22:37
  • if you do not need a column then remove entire WHERE definition – lukas.pukenis May 30 '12 at 22:38
  • FWIW (some may find this of interest): If a `WHERE` is required (because, well, sometimes fickle systems require it sometimes...) then `WHERE 1=1` will "match everything". Likewise this can be extended (once again, for those fickle systems) `WHERE 1=1 OR c='don''t care'`... –  May 30 '12 at 22:40
  • @stevether Only that which is constrained via parameterized queries ;-) –  May 30 '12 at 22:48
  • 21
    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 '13 at 22:49
  • 4
    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. – Andrew Brown May 27 '16 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 '17 at 23:17
  • Is there a way to do it using `sequelize`? – Eli Zatlawy Dec 01 '22 at 15:04

15 Answers15

48

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.

DubDub
  • 1,277
  • 1
  • 10
  • 24
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?

Community
  • 1
  • 1
Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
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%'
Eugen Rieck
  • 64,175
  • 10
  • 70
  • 92
  • 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. – gavin stanley Jul 23 '18 at 11:46
  • @gavin What's your programming language? – Eugen Rieck Jul 24 '18 at 07:32
  • hi, MySQL. I have written 2 queries, but was interested in a value or symbol that meant EVERYTHING, much like the OP – gavin stanley Jul 24 '18 at 14:32
  • Yes I am having this exact same need, and also I have not seen an answer as the LIKE clause doesn't return Nulls. – Julio Arriaga Jun 06 '19 at 21:37
  • 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 '19 at 19:55
  • 2
    @Truth - no, in the end I wrote two queries - `if (var=all){query without WHERE}else {query with WHERE = var}` – gavin stanley Jun 22 '19 at 08:29
  • 1
    @gavin - Ha! I came up with a very similar solution before you wrote back. See below. – Truth Jun 23 '19 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 '19 at 20:21
  • @Ivan Abusing a prepared statement this way would be a huge mistake. – Eugen Rieck May 23 '20 at 13:10
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

FanoFN
  • 6,815
  • 2
  • 13
  • 33
mahmoodi.saeed
  • 111
  • 1
  • 6
  • 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 '20 at 19:52
8

Everything or anything?

You could use a wildcard I guess.

SELECT * FROM table WHERE column LIKE "%"
Steve Robbins
  • 13,672
  • 12
  • 76
  • 124
  • 6
    Nope, you're wrong. `= '%'` means "the value exactly equal to one percent char" – zerkms May 30 '12 at 22:38
  • 2
    And you're wrong once again, because it wouldn't include `NULL` values – zerkms May 30 '12 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 '12 at 22:39
  • (I wonder what the point of that is... although, perhaps for some brittle SQL generators...) –  May 30 '12 at 22:39
  • I wanted to create some dynamic SQL, but that's fine, I know of a better way of doing it. – Majo0od May 30 '12 at 22:45
  • 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 04 '13 at 17:53
5
SELECT * FROM table WHERE column = IF (? = '', column, ?);
Truth
  • 486
  • 7
  • 19
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');
Mostafa Norzade
  • 1,578
  • 5
  • 24
  • 40
gazarsgo
  • 104
  • 2
  • 12
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

Olamide226
  • 428
  • 6
  • 12
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}}

Roman
  • 411
  • 3
  • 10
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);
Warkaz
  • 845
  • 6
  • 18
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

Marius Illmann
  • 302
  • 3
  • 5
  • 14
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

Hamreen Ahmad
  • 522
  • 5
  • 21
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;
ethry
  • 731
  • 6
  • 17