创建VIEW时出现Mysql2语法错误

时间:2016-08-23 04:48:20

标签: mysql ruby-on-rails bash mysql2 windows-subsystem-for-linux

我刚刚在Windows 10上安装了Bash,安装了libmysqlclient-dev软件包并运行了一个执行以下查询的rake任务,使用ActiveRecord :: Base.connection.execute <在mysql数据库上创建VIEW / p>

DROP TABLE IF EXISTS debtors_customer_balances;
CREATE OR REPLACE VIEW debtors_customer_balances AS
  SELECT
    customer_id,
    SUM(amount_cents) AS total_cents,
    SUM(CASE
          WHEN due_on >= CURDATE()
          THEN amount_cents
          ELSE 0
          END) AS current_cents,
    SUM(CASE
          WHEN due_on >= (CURDATE() - INTERVAL 7 DAY)
          AND  due_on < CURDATE()
          THEN amount_cents
          ELSE 0
          END) AS overdue7_cents,
    SUM(CASE
          WHEN due_on >= (CURDATE() - INTERVAL 14 DAY)
          AND  due_on < (CURDATE() - INTERVAL 7 DAY)
          THEN amount_cents
          ELSE 0
          END) AS overdue14_cents,
    SUM(CASE
          WHEN due_on >= (CURDATE() - INTERVAL 30 DAY)
          AND  due_on < (CURDATE() - INTERVAL 14 DAY)
          THEN amount_cents
          ELSE 0
          END) AS overdue30_cents,
    SUM(CASE
          WHEN due_on < (CURDATE() - INTERVAL 30 DAY)
          THEN amount_cents
          ELSE 0
          END) AS overdue30_plus_cents
  FROM
    debtors_balances
  GROUP BY
    customer_id;

然而,它正在抛出错误

  

Mysql2 ::错误:您的SQL语法中有错误;检查手册   对应于您的MySQL服务器版本,以获得正确的语法   使用near&#39;创建或替换视图debtors_customer_balances AS SELECT   &#39;在第2行:DROP TABLE IF EXISTS debtors_customer_balances;创建或   REPLACE VIEW debtors_customer_balances AS SELECT       customer_id等......

我无法弄清楚导致这种情况的原因是因为查询在Mac上运行良好,它只是在bash for windows中我似乎得到了这种语法错误。

我使用的宝石是mysql2(0.3.18)

1 个答案:

答案 0 :(得分:0)

这是由第一行DROP TABLE IF EXISTS debtors_customer_balances;引起的,实际应该是DROP VIEW ...。删除第一行,它应该工作。无论如何,您正在使用CREATE OR REPLACE VIEW debtors_customer_balances那么之前添加DROP语句的含义是什么?