【问题标题】:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'C3714552' in 'where clause'com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:“where 子句”中的未知列“C3714552”
【发布时间】:2018-05-04 16:52:54
【问题描述】:
Statement st1 = conn.createStatement();
ResultSet rs2 = st1.executeQuery("
   SELECT cui_2 
   FROM diagnostico.disease_has_symptom, diagnostico.symptom
   WHERE(
      `symptom`.`cui` = `disease_has_symptom`.`cui_2` AND 
      `symptom`.`cui` = " + sintomas.pop() + ");
");

我不知道为什么会收到此错误消息...

【问题讨论】:

  • 我猜 sintomas 包含字符串,您需要在查询中用引号将其分隔。
  • sintomas.pop() 是否等于该值?您可能需要在其周围加上单引号
  • 是的,就是这样!干杯队友!!!

标签: mysql sql


【解决方案1】:

正如@Brad 所指出的,您的主要问题涉及混淆引号,`symptom`.`cui` = " + sintomas.pop() + "); 应该是:`symptom`.`cui` = ' + sintomas.pop() + ');

为什么会这样?根据SQL-ANSI 92(大多数RDBMS 遵守的SQL 语言的正式行业标准的旧版本,包括SQL Server、Oracle、Postgres、MySQL 等),单引号和双引号用于不同的目的。

双引号用于标识符,包括列名和表名,例如"symptom"."cui"

SQL-92 doc的5.2节下,下面是分隔标识符的函数定义:

<delimited identifier> ::=
   <double quote> <delimited identifier body> <double quote>

以下是这种类型的几个常见用途:

  • 双引号允许空格,根据 5.2 - 语法规则

    2) With the exception of the <space> character explicitly contained
        in <timestamp string> and <interval string> and the permitted
        <separator>s in <bit string literal>s and <hex string literal>s,
        a <token>, other than a <character string literal>, a <national
        character string literal>, or a <delimited identifier>, shall not 
        include a <space> character or other <separator>.
    
  • 与常规标识符相比,双引号会区分大小写:

    13)A <regular identifier> and a <delimited identifier> are equivalent 
        if the <identifier body> of the <regular identifier> (with
        every letter that is a lower-case letter replaced by the equiva-
        lent upper-case letter or letters) and the <delimited identifier
        body> of the <delimited identifier> (with all occurrences of
        <quote> replaced by <quote symbol> and all occurrences of <dou-
        blequote symbol> replaced by <double quote>), considered as
        the repetition of a <character string literal> that specifies a
        <character set specification> of SQL_TEXT and an implementation-
        defined collation that is sensitive to case, compare equally
        according to the comparison rules in Subclause 8.2, "<comparison
        predicate>".
    

但是,一些 RDBMS 有自己的特殊字符、空格和reserved words 的转义符号,它们不是 ANSI 标准。例如,SQL Server 可以使用方括号[...]; MySQL 可以使用反引号`...`


单引号用于将字符串文字括在 char、varchar、文本数据类型列中,例如 'C3714552'。由于您使用了双引号,因此引发了您的错误消息,并且 MySQL 引擎正在寻找列标识符,因为您将值括在双引号中。

SQL-92 doc 的 5.3 以下是字符串文字的定义(不存在双引号):

 <character string literal> ::=
      [ <introducer><character set specification> ]
      <quote> [ <character representation>... ] <quote>
        [ { <separator>... <quote> [ <character representation>... ] <quote> }... ]

并且在 5.3 - 语法规则下:

 1) In a <character string literal> or <national character string
    literal>, the sequence:

      <quote> <character representation>... <quote>
      <separator>... <quote> <character representation>... <quote>

    is equivalent to the sequence

      <quote> <character representation>... <character representa-
      tion>... <quote>

话虽如此,MySQL 提供了各种非 ANSI modes,它们可能对字符串文字使用双引号。但可能是 JDBC 或 ODBC 等后端 API,驱动程序默认为 ANSI 标准。

【讨论】:

    猜你喜欢
    • 2015-11-11
    • 2014-05-23
    • 1970-01-01
    • 1970-01-01
    • 2018-01-28
    • 2011-12-15
    • 1970-01-01
    • 2014-05-31
    • 1970-01-01
    相关资源
    最近更新 更多