Development Tip

아무것도 선택하지 않으시겠습니까?

yourdevel 2020. 10. 11. 11:25
반응형

아무것도 선택하지 않으시겠습니까?


다음과 같은 진술을 할 수 있습니까?

SELECT "Hello world"
WHERE 1 = 1

SQL에서?

내가 알고 싶은 가장 중요한 것은 아무것도 선택하지 않아도된다는 것입니다. 즉, FROM 절이 없습니다.


공급 업체간에 일관성이 없습니다. Oracle, MySQL 및 DB2는 이중을 지원합니다.

SELECT 'Hello world'
  FROM DUAL

... SQL Server, PostgreSQL 및 SQLite에는 다음이 필요하지 않습니다 FROM DUAL.

SELECT 'Hello world'

MySQL은 두 가지 방법을 모두 지원합니다.


에서 오라클 :

SELECT 'Hello world' FROM dual

SQL Server의 이중 대응 :

SELECT 'Hello world' 

이 시도.

단일:

SELECT *  FROM (VALUES ('Hello world')) t1 (col1) WHERE 1 = 1

다중:

SELECT *  FROM (VALUES ('Hello world'),('Hello world'),('Hello world')) t1 (col1) WHERE 1 = 1

자세한 내용은 http://modern-sql.com/use-case/select-without-from


SQL Server 유형 :

Select 'Your Text'

FROMor WHERE절이 필요하지 않습니다 .


할 수 있습니다. StackExchange Data Explorer 쿼리 에서 다음 줄을 사용하고 있습니다 .

SELECT
(SELECT COUNT(*) FROM VotesOnPosts WHERE VoteTypeName = 'UpMod' AND UserId = @UserID AND PostTypeId = 2) AS TotalUpVotes,
(SELECT COUNT(*) FROM Answers WHERE UserId = @UserID) AS TotalAnswers

데이터 교환은 Transact-SQL (SQL에 대한 SQL Server 독점 확장)을 사용합니다.

다음과 같은 쿼리를 실행하여 직접 시도 할 수 있습니다.

SELECT 'Hello world'

다음은 https://blog.jooq.org/tag/dual-table/ 에서 이중 데이터베이스 지원의 가장 완전한 목록입니다 .

다른 많은 RDBMS에서는 다음과 같은 명령문을 실행할 수 있으므로 더미 테이블이 필요하지 않습니다.

SELECT 1;
SELECT 1 + 1;
SELECT SQRT(2);

다음은 RDBMS이며 일반적으로 위의 작업이 가능합니다.

  • H2
  • MySQL
  • Ingres
  • Postgres
  • SQLite
  • SQL 서버
  • Sybase ASE

다른 RDBMS에서는 Oracle과 같이 더미 테이블이 필요합니다. 따라서 다음과 같이 작성해야합니다.

SELECT 1       FROM DUAL;
SELECT 1 + 1   FROM DUAL;
SELECT SQRT(2) FROM DUAL;

다음은 RDBMS 및 해당 더미 테이블입니다.

  • DB2 : SYSIBM.DUAL
  • 더비 : SYSIBM.SYSDUMMY1
  • H2 : 선택적으로 DUAL 지원
  • HSQLDB : INFORMATION_SCHEMA.SYSTEM_USERS
  • MySQL : 선택적으로 DUAL 지원
  • 오라클 : DUAL
  • Sybase SQL Anywhere : SYS.DUMMY

Ingres에는 DUAL이 없지만 실제로 Ingres에서는 FROM 절없이 WHERE, GROUP BY 또는 HAVING 절을 사용할 수 없으므로 실제로 필요합니다.


불가능하다고 생각합니다. 이론적으로 : select는 두 가지 작업을 수행합니다.

  • 세트를 좁히거나 쪼개기 (세트 이론);

  • 결과 매핑.

첫 번째는 수직 감소로 볼 수있는 where 절에 반대되는 수평 감소로 볼 수 있습니다. 반면에 조인은 집합을 가로로 늘릴 수 있으며 유니온은 집합을 세로로 늘릴 수 있습니다.

               augmentation          diminishing
horizontal     join/select              select   
vertical          union            where/inner-join

두 번째는 매핑입니다. 매핑은 변환기에 가깝습니다. SQL에서는 일부 필드를 사용하고 0 개 이상의 필드를 반환합니다. 선택에서 sum, avg 등과 같은 일부 집계 함수를 사용할 수 있습니다. 또는 모든 열 값을 가져 와서 문자열로 변환 할 수 있습니다. C # linq에서는 select가 T 유형의 객체를 받아들이고 U 유형의 객체를 반환한다고 말합니다.

I think the confusion comes by the fact that you can do: select 'howdy' from <table_name>. This feature is the mapping, the converter part of the select. You are not printing something, but converting! In your example:

SELECT "
WHERE 1 = 1

you are converting nothing/null into "Hello world" and you narrow the set of nothing / no table into one row, which, imho make no sense at all.

You may notice that, if you don't constrain the number of columns, "Hello world" is printed for each available row in the table. I hope, you understand why by now. Your select takes nothing from the available columns and creates one column with the text: "Hello world".

So, my answer is NO. You can't just leave out the from-clause because the select always needs table-columns to perform on.


In Standard SQL, no. A WHERE clause implies a table expression.

From the SQL-92 spec:

7.6 "where clause"

Function

Specify a table derived by the application of a "search condition" to the result of the preceding "from clause".

In turn:

7.4 "from clause"

Function

Specify a table derived from one or more named tables.

A Standard way of doing it (i.e. should work on any SQL product):

SELECT DISTINCT 'Hello world' AS new_value
  FROM AnyTableWithOneOrMoreRows
 WHERE 1 = 1;

...assuming you want to change the WHERE clause to something more meaningful, otherwise it can be omitted.


For ClickHouse, the nothing is system.one

SELECT 1 FROM system.one

There is another possibility - standalone VALUES():

VALUES ('Hello World');

Output:

column1
Hello World

It is useful when you need to specify multiple values in compact way:

VALUES (1, 'a'), (2, 'b'), (3, 'c');

Output:

column1     column2
      1     a
      2     b
      3     c

DBFiddle Demo

This syntax is supported by SQLite/PostgreSQL/DB LUW/MariaDB 10.3.


I know this is an old question but the best workaround for your question is using a dummy subquery:

SELECT 'Hello World'
FROM (SELECT name='Nothing') n
WHERE 1=1

This way you can have WHERE and any clause (like Joins or Apply, etc.) after the select statement since the dummy subquery forces the use of the FROM clause without changing the result.


I'm using firebird First of all, create a one column table named "NoTable" like this

CREATE TABLE NOTABLE 
(
  NOCOLUMN              INTEGER
);
INSERT INTO NOTABLE VALUES (0); -- You can put any value

now you can write this

select 'hello world' as name

from notable

you can add any column you want to be shown


In Firebird, you can do this:

select "Hello world" from RDB$DATABASE;

RDB$DATABASE is a special table that always has one row.


For DB2:

`VALUES('Hello world')`

You can do multiple "rows" as well:

`VALUES('Hello world'),('Goodbye world');`

You can even use them in joins as long as the types match:

VALUES(1,'Hello world')
UNION ALL
VALUES(2,'Goodbye world');

참고URL : https://stackoverflow.com/questions/3732422/select-from-nothing

반응형