SQL WHERE column = everything

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.


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?


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%'

Tags:

Mysql

Sql