SQL-从多个表创建视图 [英] SQL - Create view from multiple tables

查看:986
本文介绍了SQL-从多个表创建视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个表:

POP(country, year, pop)
FOOD(country, year, food)
INCOME(country, year, income)

我正在尝试创建一个视图,例如:

I am trying to create a view such as:

V(country, year, pop, food, income)

到目前为止,这是我的代码.我认为这是不正确的:

This is my code so far. I don't think its correct:

CREATE VIEW V AS
(SELECT * FROM POP
UNION
SELECT * FROM FOOD
UNION
SELECT * FROM INCOME);

一个问题是,POP中存在的国家可能不在FOOD中.一年也是如此. POP中存在的年份可能在FOOD中不存在.因此,我一直以为上面的代码行不通.

One issue is, a country that is present in POP may not be present in FOOD. Same goes for year. A year that is present in POP may not be present in FOOD. So, I keep thinking that the above code will not work.

推荐答案

感谢您的帮助.这就是我最终为了使其工作而要做的事情.

Thanks for the help. This is what I ended up doing in order to make it work.

CREATE VIEW V AS
    SELECT *
    FROM ((POP NATURAL FULL OUTER JOIN FOOD)
    NATURAL FULL OUTER JOIN INCOME);

这篇关于SQL-从多个表创建视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆