为什么要在SQL中使用WHERE 1=0语句?[英] why would you use WHERE 1=0 statement in SQL?

本文是小编为大家收集整理的关于为什么要在SQL中使用WHERE 1=0语句?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我在应用程序上的日志文件中看到了一个查询.它包含一个查询:

SELECT ID FROM CUST_ATTR49 WHERE 1=0

这样的查询有什么用,一定没有返回的查询?

推荐答案

这样的查询可以用于ping数据库.该子句:

WHERE 1=0

确保将非数据发送回,因此不收取CPU费用,没有网络流量或其他资源消耗.

这样的查询可以测试:

  • 服务器可用性
  • cust_attr49表格存在
  • ID列的存在
  • 保持连接活力
  • 导致触发器发射而不会更改任何行(使用White子句,但在选择查询中没有)
  • 管理动态查询中的许多或条件(例如WHERE 1=0 OR <condition>)

其他推荐答案

这也可用于从表中提取表格架,而无需在该表中提取任何数据.正如安德里亚·科洛尼(Andrea Colleoni)所说,这些将是使用此功能的其他好处.

其他推荐答案

我可以想到的用户酶:您有一个不想有任何搜索结果的过滤表单.如果指定一些过滤器,则将它们添加到Where子句中.

或通常需要手工创建SQL查询,通常使用.例如.您不想检查Where子句是否为空..您只能添加类似的内容:

where := "WHERE 0=1"

if X then where := where + " OR ... "
if Y then where := where + " OR ... "

(如果您将条款连接到或需要0 = 1,如果有1 = 1)

)

本文地址:https://www.itbaoku.cn/post/597706.html

问题描述

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 such a query that is bound to return nothing?

推荐答案

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 (with the where clause, but not in a select query)
  • manage many OR conditions in dynamic queries (e.g WHERE 1=0 OR <condition>)

其他推荐答案

This may be also used to extract the table schema from a table without extracting any data inside that table. As Andrea Colleoni said those will be the other benefits of using this.

其他推荐答案

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)