How does mysql define DISTINCT() in reference documentation

后端 未结 2 1481
太阳男子
太阳男子 2020-12-21 11:58

EDIT: This question is about finding definitive reference to MySQL syntax on SELECT modifying keywords and functions. /EDIT

AFAIK SQL defines two uses of DISTINCT k

相关标签:
2条回答
  • 2020-12-21 12:49

    For completeness sake I am answering my own and linking to another question of my own. It seems that this behaviour is a direct consequence of SQL standard allowing whitespace between the function and parenthesis.

    Since it is (generally) allowed to say FUNCTION_NAME (x) then when this function is applied to a first term of select

    SELECT FUNCTION_NAME (x)
    

    then there parser is going to have a hard time establishing if this is a context of a function name or SELECT modifying keyword.

    So in the above case the FUNCTION_NAME is actually FUNCTION_NAME_OR_KEYWORD to the parser.

    But it goes further: since the space between function name and parenthesis IS allowed the the parser actually can NOT distinguish between

    SELECT FUNCTION_NAME_OR_KEYWORD (x)
    

    and

    SELECT FUNCTION_NAME_OR_KEYWORD(x)
    

    (it must test the keywords to see if they are functions), and since (x) will be parsed to x it follows that for FUNCTION_NAME_OR_KEYWORD -> DISTINCT (and all other SELECT modifying keywords) there is no difference between

    SELECT DISTINCT x, y, z, ...
    

    and

    SELECT DISTINCT(x), y, z, ...
    

    QED, but without hard references (assumption that standard does not care about whitespace between function names and parenthesis is, I believe, justified, but I was unable to follow BNF grammar to the point that I could quote the exact rule).

    NOTE: mysql has certain number of functions where it cares about whitespace between functions and parenthesis, but I believe that these are exceptions (hence server option to ignore it)

    0 讨论(0)
  • 2020-12-21 12:50

    Interesting scenario.

    As you found,

    SELECT DISTINCT(a), b, c
    

    is equivalent to:

    SELECT DISTINCT (a), b, c
    

    is equivalent to:

    SELECT DISTINCT a, b, c
    

    i.e. the parentheses are treated as expression grouping parentheses.

    Interestingly, a very similar issue occurs in VB6/VBScript where (for Function(byref x)) Function(x), Function x and Call Function(x) are all slightly different in which value they pass by reference (Function(x) passes a reference to the result of the (x) expression and not x).

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