简体   繁体   English

MYSQL-创建视图以查询两个相似的表

[英]MYSQL - Create View to query two like tables

I have to like tables Order_Items and Order_Items_Archived which both tables are InnoDB. 我必须喜欢两个表都是InnoDB的表Order_Items和Order_Items_Archived。 I would like to create a query that I can pull all the Items from both orders. 我想创建一个查询,可以从两个订单中提取所有商品。 I am pretty sure I would do this with a VIEW but I cant seem to find reference to just select all records that have the exact same column names and column types. 我很确定我会使用VIEW来做到这一点,但是我似乎找不到引用来选择所有具有完全相同的列名和列类型的记录。

Example: 例:

select sum(OrderItems_Amount)
from Order_Items_View
where OrderItems_OrderDate = '2017-10-01';

Sounds like you're looking for the union all operator: 听起来您正在寻找union all运算符:

CREATE OR REPLACE VIEW order_items_view AS
SELECT * FROM order_items
UNION ALL
SELECT * FROM order_items_archived

You would use a union all query. 您将使用并union all查询。 I would be inclined to add the source. 我倾向于添加源。 List the columns that you want: 列出所需的列:

select col1, col2, col3, . . . , 0 as is_archive
from order_items
union all
selecct col1, col2, col3, . . ., 1
from order_items_archive;

You can put a create view statement before the query to turn it into a view. 您可以在查询之前放置一个create view语句,以将其变为视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM