从3个表创建视图时出错

时间:2019-06-12 05:00:57

标签: mysql database tableview

我必须在MySQL中创建一个视图,这是代码:

CREATE VIEW dashboard_sales AS (
    SELECT o.order_id,o.order_date,o.order_status,op.op_status,oi.oi_qty 
    FROM
        order o
    LEFT JOIN
        order_items oi
    ON
        o.order_id = oi.order_id
    LEFT JOIN
        order_payment op
    ON 
        o.order_id = op.order_id
    GROUP BY o.order_id
);

但是当我在phpmyadmin中执行时,出现错误:

  

1064-您的SQL语法有错误;检查与您的MariaDB服务器版本相对应的手册以获取在'order o附近使用正确的语法           左联接               order_items oi           上               o.order_id = oi。”在第4行

如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

您需要对order

进行复选
CREATE VIEW dashboard_sales AS (
    SELECT o.order_id,o.order_date,o.order_status,op.op_status,oi.oi_qty 
    FROM `order` o LEFT JOIN order_items oi
    ON o.order_id = oi.order_id
    LEFT JOIN order_payment op
    ON o.order_id = op.order_id
);

答案 1 :(得分:0)

使用关键字作为表格名称时,应将表格放在方括号之间。

CREATE VIEW dashboard_sales AS (
SELECT o.order_id,o.order_date,o.order_status,op.op_status,oi.oi_qty 
FROM
    [order] o
LEFT JOIN
    order_items oi
ON
    o.order_id = oi.order_id
LEFT JOIN
    order_payment op
ON 
    o.order_id = op.order_id
GROUP BY o.order_id

);