代码之家  ›  专栏  ›  技术社区  ›  Oscar A Garcia

MySQL DB query抛出错误说我有语法错误。好像找不到他们?我相信它在查询的列部分

  •  0
  • Oscar A Garcia  · 技术社区  · 6 年前

    我正在创建原始的MySQL查询,以便使用Laravel'DB'facade插入到数据库中。我已经得到了相同类型的查询来处理另一个数据库表,但是它在这个特定的表上抛出了错误。

     1   PDOException::("SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'group,originating_attorney,practice_area,responsible_attorney,statute_of_limitat' at line 1")
      /home/vagrant/Code/proj/vendor/laravel/framework/src/Illuminate/Database/Connection.php:452
    
    2   PDO::prepare("INSERT INTO firm_matters(id,etag,display_number,custom_number,description,status,location,client_reference,billable,maildrop_address,billing_method,open_date,close_date,pending_date,client,contingency_fee,custom_rate,group,originating_attorney,practice_area,responsible_attorney,statute_of_limitations,user,account_balances,custom_field_vals,custom_field_set_associations,created_at,updated_at) VALUES (...)
    

    我已经检查了MySQL的保留关键字,我认为我没有违反任何规则。有没有更精通MySQL的人可以检查一下我的查询是否正确。

    3 回复  |  直到 6 年前
        1
  •  3
  •   Peter    6 年前

    我已经检查了MySQL的保留关键字

    不,你没有

     syntax to use near 'group,originating
                         ^^^^^ here
    

    GROUP 是保留字 https://dev.mysql.com/doc/refman/5.7/en/keywords.html

    只要不要创建“原始MySQL查询”,下次就可以了。你很聪明,可以使用框架,但你使用的是“原始MySQL查询”。别这么做。

        2
  •  2
  •   Bill Karwin    6 年前

    检查对应于MySQL服务器版本的手册,以获得在'group,…'附近使用的正确语法。。。

    这里有一个关于MySQL语法错误的提示:当SQL解析器在查询中遇到一个使其混淆的单词时,错误消息会准确地显示出它被混淆的位置。在这种情况下,单词 group 是语法分析器在查询中的那个位置没有预料到的。

    其他人已经回答了 是MySQL中的保留字。如果将保留字放在后勾号中以分隔它们,并明确它们是标识符,而不是SQL关键字,则可以将保留字用作标识符(表名、列名等)。

    INSERT INTO firm_matters(id, etag, display_number, custom_number, description, 
      status, location, client_reference, billable, maildrop_address, billing_method,
      open_date, close_date, pending_date, client, contingency_fee, custom_rate,
      `group`,
      originating_attorney, practice_area,responsible_attorney, statute_of_limitations, 
      user, account_balances, custom_field_vals, custom_field_set_associations,
      created_at, updated_at) VALUES (...)
    

    @Peter推荐使用一个框架是因为许多框架会自动在back ticks中分隔所有标识符,以防它们是保留字。

    但是您可以自己解决这个错误,而不需要采用复杂的框架。要么界定标识符,至少是那些匹配MySQL保留字的标识符,要么首先不要使用保留字作为标识符。

    直接编写SQL没有错。

        3
  •  -1
  •   kenken9999    6 年前

    取决于MySQL版本,至少group是保留关键字

    https://dev.mysql.com/doc/refman/5.7/en/keywords.html#keywords-5-7-detailed-G

    当您编写原始查询时,请更改

    column to `column`