从不同列数的表中创建视图mysql

  • 本文关键字:创建 视图 mysql mysql sql
  • 更新时间 :
  • 英文 :


我是MySQL服务器视图的新手。

我正在尝试创建两个视图"invoice_head"one_answers"invoice_body",但为了做到这一点,我需要从不同的表(这些表有不同的列数)中获取不同的键。

这是我要使用的代码:

CREATE VIEW invoice_head AS
SELECT
date_order AS 'DATE OF ISSUE'
FROM orders
UNION ALL
SELECT
customer_name AS 'Client Name',
customer_address AS 'Street adress', 
customer_district AS 'District', 
customer_country AS 'Portugal', 
customer_zipcode AS 'ZIP Code'
FROM customer;

但是这段代码给了我错误:使用的SELECT语句有不同的列数。

我使用的表是这样的(例如)

客户表(8列)

<表类>customer_idcustomer_namecustomer_addresscustomer_district(…)tbody><<tr>1玛丽亚xxx街xxx《里斯本条约》(…)

评论中有人帮助了我。我只需要使用"Join"而不是"Select":

DROP VIEW IF EXISTS `invoice_head`;
CREATE VIEW `invoice_head` AS
SELECT  date_order as `Date of issue`,
customer_name AS `Client Name`,
customer_address AS `Street adress`, 
customer_district AS `District`, 
customer_country AS `Country`, 
customer_zipcode AS `ZIP Code`
FROM orders
join customer
on orders.customer_id = customer.customer_id;

最新更新