是否有一个SQL查询将总是返回零结果? [英] Is there a SQL query that will always return zero results?

查看:103
本文介绍了是否有一个SQL查询将总是返回零结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果有MySQL / PostgreSQL / Oracle特定的解决方案,我很好奇他们所有。

If there is a MySQL/PostgreSQL/Oracle-specific solution, I'm curious about them all.

推荐答案

在DBMS上,以下中的一个或多个将工作:

Depending on the DBMS, one or more of the following will work:


  • SELECT NULL LIMIT 0 (PostgreSQL和MySQL语法)/ SELECT TOP 0 1 (MS SQL Server语法)

  • SELECT NULL WHERE FALSE (DBMS带有布尔类型,例如PostgreSQL) SELECT NULL WHERE 1 = 0 (大多数DBMS)

  • SELECT NULL LIMIT 0 (PostgreSQL and MySQL syntax) / SELECT TOP 0 1 (MS SQL Server syntax)
  • SELECT NULL WHERE FALSE (DBMS with a boolean type, e.g. PostgreSQL) SELECT NULL WHERE 1=0 (most DBMSes)

对于Oracle,这些将需要是 SELECT NULL FROM DUAL 的形式,不能有 SELECT 没有 FROM 子句;不确定 LIMIT / TOP WHERE 它将接受。

For Oracle, these will need to be of the form SELECT NULL FROM DUAL, I believe, as you can't have SELECT without a FROM clause of some sort; not sure which versions of the LIMIT / TOP and WHERE it will accept.

一个更精细的选项是创建一个(临时)表,不插入任何行,它可以给你任意数量的列,有类型关联,即使它们不包含值:

A more elaborate option is to create a (temporary) table and not insert any rows into it, which can give you any number of columns, which will have types associated even though they contain no values:

-- PostgreSQL
CREATE TEMP TABLE dummy ( a Int, b VarChar(42) );
SELECT * FROM dummy;

-- MS SQL Server
CREATE TABLE #DUMMY ( a Int, b VarChar(42) );
SELECT * FROM #DUMMY;

在PostgreSQL中,您甚至可以创建一个没有列的表, 零行零列

In PostgreSQL, you can even create a table with no columns, allowing you to have a result set with zero rows and zero columns:

CREATE TEMP TABLE empty ();
SELECT * FROM empty;

另一种可能性是,如果DBMS有set-returning函数,它们可能能够返回一个空集。例如,再次在PostgreSQL中,因为它是我最了解的,你可以给一个无效的范围 generate_series()

Another possibility is if the DBMS has set-returning functions, they may be able to return an empty set. For instance, again in PostgreSQL as it's what I know best, you can give an invalid range to generate_series():

SELECT * FROM generate_series(0,-1);

这篇关于是否有一个SQL查询将总是返回零结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆