SELECT from nothing?

前端 未结 14 2179
难免孤独
难免孤独 2020-12-24 04:03

Is it possible to have a statement like

SELECT \"Hello world\"
WHERE 1 = 1

in SQL?

The main thing I want to know, is can I SELECT f

相关标签:
14条回答
  • 2020-12-24 04:46

    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

    0 讨论(0)
  • 2020-12-24 04:48

    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.

    0 讨论(0)
  • 2020-12-24 04:50

    Here is the most complete list of database support of dual from https://blog.jooq.org/tag/dual-table/:

    In many other RDBMS, there is no need for dummy tables, as you can issue statements like these:

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

    These are the RDBMS, where the above is generally possible:

    • H2
    • MySQL
    • Ingres
    • Postgres
    • SQLite
    • SQL Server
    • Sybase ASE

    In other RDBMS, dummy tables are required, like in Oracle. Hence, you’ll need to write things like these:

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

    These are the RDBMS and their respective dummy tables:

    • DB2: SYSIBM.DUAL
    • Derby: SYSIBM.SYSDUMMY1
    • H2: Optionally supports DUAL
    • HSQLDB: INFORMATION_SCHEMA.SYSTEM_USERS
    • MySQL: Optionally supports DUAL
    • Oracle: DUAL
    • Sybase SQL Anywhere: SYS.DUMMY

    Ingres has no DUAL, but would actually need it as in Ingres you cannot have a WHERE, GROUP BY or HAVING clause without a FROM clause.

    0 讨论(0)
  • 2020-12-24 04:50

    In Firebird, you can do this:

    select "Hello world" from RDB$DATABASE;
    

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

    0 讨论(0)
  • 2020-12-24 04:51

    It's not consistent across vendors - Oracle, MySQL, and DB2 support dual:

    SELECT 'Hello world'
      FROM DUAL
    

    ...while SQL Server, PostgreSQL, and SQLite don't require the FROM DUAL:

    SELECT 'Hello world'
    

    MySQL does support both ways.

    0 讨论(0)
  • 2020-12-24 04:52

    In SQL Server type:

    Select 'Your Text'
    

    There is no need for the FROM or WHERE clause.

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