【问题标题】:Alias name on expression not recognized无法识别表达式上的别名
【发布时间】:2014-04-09 17:20:23
【问题描述】:

我正在用 JAVA 编写一个演示代码 sn-p,数据库值为 NULL

我在其中使用NULL 编写了一些简单的表达式,并为这些表达式提供了别名。但没有AS 关键字。我很清楚它是可选的。

当我在MySQL 命令行运行 SQL 查询并使用JAVA 时,结果是相同的。

但奇怪的是,(尽管可能不适合你们中的某些人),我发现在其中一个表达式上它没有使用定义的别名,而是使用了整个表达式以及表达式名称,并带有空格字符在他们之间。该空格是 sql 语句中表达式和别名之间的分隔符。

当我在同一个表达式上显式使用 AS 关键字时,结果符合预期。

使用的 SQL 语句

select 
    length( null ) null_len -- to check if null data has any length
  , length( '' ) empty_len  -- to check length of an empty string
  , null = '' 'n=e?'        -- to check if null is equal to an empty string
  , null = '' "n=e?"        -- same as above but alias name within dbl quotes
  , null = '' as 'n=e??'    -- same with AS keyword on alias name used
  , '' is null 'e=n?'       -- to check if empty is equal to null
from dual;

MySQL 命令行控制台上的结果

+----------+-----------+------------------+------------------+-------+------+
| null_len | empty_len | null = '' 'n=e?' | null = '' "n=e?" | n=e?? | e=n? |
+----------+-----------+------------------+------------------+-------+------+
|     NULL |         0 |             NULL |             NULL |  NULL |    0 |
+----------+-----------+------------------+------------------+-------+------+

使用 Java 和 ResultSetMetaData 的结果

for( int i=1; i <= numberOfColumns; i++ ) {
  String columnLabel     = rsmd.getColumnLabel( i );
  String columnName      = rsmd.getColumnLabel( i );
  //String columnType      = rsmd.getColumnTypeName( i );
  //String columnClassName = rsmd.getColumnClassName( i );

  System.out.println( "Column Label: " + columnLabel );
  System.out.println( "Column Name : " + columnLabel );
  //System.out.println( "Column Type : " + columnType );
  //System.out.println( "Column Class: " + columnClassName );

  System.out.println();
} // for count of columns

输出

Column Label: null_len
Column Name : null_len

Column Label: empty_len
Column Name : empty_len

Column Label: null='' 'n=e?'
Column Name : null='' 'n=e?'

Column Label: null='' "n=e?"
Column Name : null='' "n=e?"

Column Label: n=e??
Column Name : n=e??

Column Label: e=n?
Column Name : e=n?

问题(可能不是!)

为什么没有AS 关键字就不能识别别名名称?
这是一个已知的事实和问题吗?

注意:我用过JAVA,只是为了测试和演示问题。问题不在于 JAVA 的行为。因此,答案不必是JAVA 特定的。

【问题讨论】:

    标签: mysql sql


    【解决方案1】:

    http://dev.mysql.com/doc/refman/5.6/en/string-literals.html

    彼此相邻放置的带引号的字符串连接到单个字符串。

    这样做的一个模糊用途是允许我们将长字符串放入结果集中,但使列标题成为长字符串的较短前缀。很奇怪,但可能。我承认我从未使用过此功能。

    mysql> select 'abc' 'def';
    +--------+
    | abc    |
    +--------+
    | abcdef |
    +--------+
    

    要执行您想要的操作,您可以使用 AS 关键字,或者将别名放在反引号中作为分隔标识符而不是字符串文字。

    mysql> select 'abc' `def`;
    +-----+
    | def |
    +-----+
    | abc |
    +-----+
    

    【讨论】:

    • 我很久以前就读过文档,但想不起来了。您的第一个示例又提出了一个查询!为什么'abc' 仅被视为列标题,而不是'abc' 'def',如我发布的查询结果中所见。?
    • 好问题,我不确定。它可能是 SQL 规范规定的。
    • 我找不到这样的规范。如果你遇到了,也请附上答案。
    • 我在 SQL:2003 Foundation 文档的第 7.12 节,语法规则 12.o.ii 中找到了它(如果您正在家里阅读)。它说它依赖于实现。因此,SQL 数据库的每个实现者都可以决定它应该如何工作。
    【解决方案2】:

    似乎撇号和引号都不是转义列名的好习惯。我的建议是使用重音,它似乎在所有情况下都可以正常工作:

    select 
        length( null ) null_len -- to check if null data has any length
      , length( '' ) empty_len  -- to check length of an empty string
      , null = '' `n=e?`        -- to check if null is equal to an empty string
      , null = '' as `n=e??`    -- same with AS keyword on alias name used
      , '' is null `e=n?`       -- to check if empty is equal to null
    

    Here 结果显示在 SQL Fiddle 中。可以在here找到更多细节@

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-02-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-20
      相关资源
      最近更新 更多