【问题标题】:Update syntax error更新语法错误
【发布时间】:2014-01-25 14:52:44
【问题描述】:

是否可以在更新查询中插入来自字段? 我需要它,因为在更新某些东西之前,我需要做一些控制。 p.s 我知道我必须使用准备好的语句,但我想知道这个错误,我想这是一个缺少的报价,我看不到。 错误:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 'from warehouse,product where warehouse.idProduct=shop.idProduct and warehouse.id' at line 1

int rs = st.executeUpdate("UPDATE shop SET quantity='"
                        + Quantity
                        + "' from warehouse,product where  
                         warehouse.idProduct=shop.idProduct and 
                         warehouse.idProduct=product.id and product.brand='"
                        + Brand + "' and product.productType='" + Product
                        + "'");

【问题讨论】:

  • 如果您知道应该使用准备好的语句,为什么不这样做呢?在准备好的声明中,无论如何您都不会得到所有这些引号,因此问题可能会消失。请注意,您提供给我们的代码不是有效的 Java...
  • 我以为我可以从查询中进行更新,但我错了,无论如何我意识到准备好的语句更容易编写并且更受保护

标签: java mysql sql syntax


【解决方案1】:

这不是有效的 SQL。 UPDATE 语句不能有 FROM 子句。

Single-table syntax:

UPDATE [LOW_PRIORITY] [IGNORE] table_reference
    SET col_name1={expr1|DEFAULT} [, col_name2={expr2|DEFAULT}] ...
    [WHERE where_condition]
    [ORDER BY ...]
    [LIMIT row_count]

Multiple-table syntax:

UPDATE [LOW_PRIORITY] [IGNORE] table_references
    SET col_name1={expr1|DEFAULT} [, col_name2={expr2|DEFAULT}] ...
    [WHERE where_condition]

【讨论】:

  • 如果我想在更新前检查一些东西,我该怎么做?
  • 做一个单独的SELECT 语句,然后根据结果做你的UPDATE
【解决方案2】:

我想你想要一个加入的更新:

UPDATE shop s join
       warehouse w
       on w.idProduct = s.idProduct join
       product p
       on w.idProduct = p.id and
          p.productType = '"+Product+"' and
          p.brand = '" + Brand + "'
    SET s.quantity = "+ Quantity

作为注释。我猜Quantity 是数字。如果是这样,您不需要在值周围加上引号。

【讨论】:

    猜你喜欢
    • 2012-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-22
    • 2018-05-13
    • 2012-05-16
    相关资源
    最近更新 更多