mysql语法错误与直接工作的查询

时间:2014-03-10 14:09:35

标签: mysql

我正在尝试使用rails执行查询:

ActiveRecord::Base.connection.execute(query.join)

我将查询构建为字符串,它看起来像这样:

UPDATE balance_lines SET parent_id = 2728740 WHERE id = 2728797;
 UPDATE balance_lines SET parent_id = 2728725 WHERE id = 2728767;
 UPDATE balance_lines SET parent_id = 2728735 WHERE id = 2728783;
 UPDATE balance_lines SET parent_id = 2728727 WHERE id = 2728770;
 UPDATE balance_lines SET parent_id = 2728751 WHERE id = 2728838;

从rails执行查询时出现错误:

  

ActiveRecord :: StatementInvalid(Mysql :: Error:你有错误   你的SQL语法;查看与MySQL对应的手册   服务器版本,用于在'UPDATE balance_lines附近使用正确的语法   SET parent_id = 2728725 WHERE id = 2728767;

但是当我从mysql UI执行它时 - 它的工作原理!有什么问题?

1 个答案:

答案 0 :(得分:3)

尝试使用CASE语句:

  UPDATE balance_lines SET parent_id = 
         CASE WHEN id = 2728797  THEN 2728740
              WHEN id = 2728767  THEN 2728725
              WHEN id = 2728783  THEN 2728735
              WHEN id = 2728770  THEN 2728727
              WHEN id = 2728838  THEN 2728751

         END
  WHERE id  in (2728797,2728767,2728783,2728770,2728838)