com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:未知列' C3714552'在' where子句'

时间:2018-05-04 16:52:54

标签: mysql sql

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() + ");
");

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

1 个答案:

答案 0 :(得分:-1)

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

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

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

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

<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有自己的特殊字符,空格和SQL-92的转义符号,这些符号不是ANSI标准。例如,SQL Server可以使用方括号[...];和MySQL可以使用反引号`...`

单引号用于将字符串文字括在char,varchar,文本数据类型列(如'C3714552')中。您引发了错误消息,因为您使用双引号,然后MySQL引擎正在查找列标识符,因为您将值换成双引号。

reserved words 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 SQL-92,它们可能会使用双引号来表示字符串文字。但可能是后端API,如JDBC或ODBC,驱动程序默认为ANSI标准。