Why would someone use WHERE 1=1 AND in a SQL clause?

后端 未结 19 2356
甜味超标
甜味超标 2020-11-22 07:08

Why would someone use WHERE 1=1 AND in a SQL clause (Either SQL obtained through concatenated strings, either view definition)

I\'ve

19条回答
  •  不知归路
    2020-11-22 07:34

    Here is a use case... however I am not too concerned with the technicalities of why I should or not use 1 = 1. I am writing a function, using pyodbc to retrieve some data from SQL Server. I was looking for a way to force a filler after the where keyword in my code. This was a great suggestion indeed:

    if _where == '': _where = '1=1'
    ...
    ...
    ...
    cur.execute(f'select {predicate} from {table_name} where {_where}')
    

    The reason is because I could not implement the keyword 'where' together inside the _where clause variable. So, I think using any dummy condition that evaluates to true would do as a filler.

提交回复
热议问题