[Postgres] Create VIEW for display join tables result

For examlpe, we have two tables:

cars:

make:

For cars table, we want to know 'make_name' instead of 'make_id', we can do with 'join' two tables. In order to make future query easy, we can create VIEW:

CREATE VIEW joined AS
SELECT cars.type, cars.cost, cars.model, make.name
  FROM cars
  INNER JOIN make ON (cars.make_id = make.id)
  ORDER BY cost DESC  LIMIT 30;

Other example:

CREATE VIEW toyotas AS
SELECT cars.type, cars.cost, cars.model, make.name
  FROM cars
  INNER JOIN make ON (cars.make_id = make.id)
  WHERE make.name = 'toyota'
  ORDER BY cost DESC  LIMIT 30;

原文地址:https://www.cnblogs.com/Answer1215/p/14562077.html