why would you use WHERE 1=0 statement in SQL?

后端 未结 12 2081
没有蜡笔的小新
没有蜡笔的小新 2020-12-08 00:48

I saw a query run in a log file on an application. and it contained a query like:

SELECT ID FROM CUST_ATTR49 WHERE 1=0

what is the use of s

相关标签:
12条回答
  • 2020-12-08 01:05

    It seems like, that someone is trying to hack your database. It looks like someone tried mysql injection. You can read more about it here: Mysql Injection

    0 讨论(0)
  • 2020-12-08 01:06

    Per user milso in another thread, another purpose for "WHERE 1=0":

    CREATE TABLE New_table_name as select * FROM Old_table_name WHERE 1 = 2;

    this will create a new table with same schema as old table. (Very handy if you want to load some data for compares)

    0 讨论(0)
  • 2020-12-08 01:08

    A query like this can be used to ping the database. The clause:

    WHERE 1=0
    

    Ensures that non data is sent back, so no CPU charge, no Network traffic or other resource consumption.

    A query like that can test for:

    • server availability
    • CUST_ATTR49 table existence
    • ID column existence
    • Keeping a connection alive
    • Cause a trigger to fire without changing any rows
    • manage many OR conditions in dynamic queries (e.g WHERE 1=0 OR <condition>)
    0 讨论(0)
  • 2020-12-08 01:08

    This is very good in metadata fetching and makes thing generic. Many DBs have optimizer so they will not actually execute it but its still a valid SQL statement and should execute on all DBs. This will not fetch any result, but you know column names are valid, data types etc. If it does not execute you know something is wrong with DB(not up etc.) So many generic programs execute this dummy statement for testing and fetching metadata.

    0 讨论(0)
  • 2020-12-08 01:08

    An example of using a where condition of 1=0 is found in the Northwind 2007 database. On the main page the New Customer Order and New Purchase Order command buttons use embedded macros with the Where Condition set to 1=0. This opens the form with a filter that forces the sub-form to display only records related to the parent form. This can be verified by opening either of those forms from the tree without using the macro. When opened this way all records are displayed by the sub-form.

    0 讨论(0)
  • 2020-12-08 01:12

    A usecase I can think of: you have a filter form where you don't want to have any search results. If you specify some filter, they get added to the where clause.

    Or it's usually used if you have to create a sql query by hand. E.g. you don't want to check whether the where clause is empty or not..and you can just add stuff like this:

    where := "WHERE 0=1"
    
    if X then where := where + " OR ... "
    if Y then where := where + " OR ... "
    

    (if you connect the clauses with OR you need 0=1, if you have AND you have 1=1)

    0 讨论(0)
提交回复
热议问题