Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL - Create view from multiple tables

I have three tables:

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);

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.

like image 375
FidelCashflo Avatar asked Dec 01 '12 05:12

FidelCashflo


People also ask

Can you create a view from multiple tables in SQL?

A view that combines data from multiple tables enables you to show relevant information in multiple tables together. You can create a view that combines data from two or more tables by naming more than one table in the FROM clause.

How can a view refer to multiple tables?

Explanation: In MySQL, a 'View' can refer to multiple tables. This makes it a lot simpler to execute the queries involving joins. When a selection is made from the view, the join is executed producing the results.

How many tables can be used in view?

The maximum number of table sources that can be joined in a single query is 256.

How do you create a table from multiple tables in SQL?

Answer: To do this, the SQL CREATE TABLE syntax is: CREATE TABLE new_table AS (SELECT * FROM old_table WHERE 1=2); For example: CREATE TABLE suppliers AS (SELECT * FROM companies WHERE 1=2);


2 Answers

This works too and you dont have to use join or anything:

DROP VIEW IF EXISTS yourview;

CREATE VIEW yourview AS
    SELECT table1.column1, 
    table2.column2
FROM 
table1, table2 
WHERE table1.column1 = table2.column1;

like image 67
Emil Wallin Avatar answered Nov 15 '22 18:11

Emil Wallin


Union is not what you want. You want to use joins to create single rows. It's a little unclear what constitutes a unique row in your tables and how they really relate to each other and it's also unclear if one table will have rows for every country in every year. But I think this will work:

CREATE VIEW V AS (

  SELECT i.country,i.year,p.pop,f.food,i.income FROM
    INCOME i
  LEFT JOIN 
    POP p 
  ON
    i.country=p.country
  LEFT JOIN
    Food f
  ON 
    i.country=f.country
  WHERE 
    i.year=p.year
  AND
    i.year=f.year
);

The left (outer) join will return rows from the first table even if there are no matches in the second. I've written this assuming you would have a row for every country for every year in the income table. If you don't things get a bit hairy as MySQL does not have built in support for FULL OUTER JOINs last I checked. There are ways to simulate it, and they would involve unions. This article goes into some depth on the subject: http://www.xaprb.com/blog/2006/05/26/how-to-write-full-outer-join-in-mysql/

like image 28
Ilion Avatar answered Nov 15 '22 17:11

Ilion