SELECT from nothing?

前端 未结 14 2181
难免孤独
难免孤独 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 05:06

    I think it is not possible. Theoretically: select performs two sorts of things:

    • narrow/broaden the set (set-theory);

    • mapping the result.

    The first one can be seen as a horizontal diminishing opposed to the where-clause which can be seen as a vertical diminishing. On the other hand, a join can augment the set horizontally where a union can augment the set vertically.

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

    The second one is a mapping. A mapping, is more a converter. In SQL it takes some fields and returns zero or more fields. In the select, you can use some aggregate functions like, sum, avg etc. Or take all the columnvalues an convert them to string. In C# linq, we say that a select accepts an object of type T and returns an object of type 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.

    0 讨论(0)
  • 2020-12-24 05:07

    For ClickHouse, the nothing is system.one

    SELECT 1 FROM system.one
    
    0 讨论(0)
提交回复
热议问题