官术网_书友最值得收藏!

Joining tables in databases

If you use a database (SpatiaLite or PostGIS) to store your data, vector and nonspatial, then you also have the option of using the database and SQL to perform tables joins. The primary advantages of this method include being able to filter data before loading in the map, perform multitable joins (three or more), and have full control over the details of the join via queries.

Getting ready

You'll need at least two layers in either a SpatiaLite or PostGIS database. These two layers need at least one column in common, and the column in common should contain unique values in at least one table. In this case, our example uses the census_wake_2000 polygon layer and census_wake_2000_pop.csv.

How to do it…

  1. Open the DB Manager plugin that comes with QGIS. You can find this in the Database menu.
  2. Select your database from the tree on the left-hand side, use cookbook.db in SpatiaLite (which was created in Chapter 1, Data Input and Output).

    Tip

    If you don't see this database listed, use Add SpatiaLite Layer (the icon or the menu item), or right-click on SpatiaLite in the Browser window to make a new connection and add it to an existing database.

  3. Now, open the SQL window (the second icon from the left in top toolbar of the plugin window).
  4. Put in the following SQL code to query and JOIN the tables:
    SELECT *
    FROM census_wake2000 Sas a
    JOIN census_wake2000_pop AS b
    ON a.stfid = b.stfid;

How it works…

SELECT lists all the columns that you want from the source tables; in this case, * means everything. FROM is the first (left) table, as a is an alias, which is used so that there's less typing later. JOIN is the second (right) table, and ON indicates which columns to should be matched between the two tables. The rest of how this works in relational database theory is best explained in other texts.

There's more…

In databases, there's more than one type of join. You can perform a join where you retain only the matches in both tables, or you can retain all content from the left (first table) and any matches from the right. You can also control how a one-to-many relationship is summarized or select specific records instead of aggregating.

If you want to save the results of a query you have two options. You can make a view or a new table. A view is a saved copy of your query. Every time you open it, the query will be rerun. This is great if your data changes because it will always be up-to-date, and this doesn't use any additional disk space. On the other hand, a table is like saving a new file; it becomes a static new copy of the results. This is good to repeatedly access the same answer, and it is usually faster to use, especially for large tables.

See also

  • Refer to the Creating views in SpatiaLite and Creating views in PostGIS sections in this chapter to learn how to make views of the query results.
  • For more general information on writing SQL queries refer to http://sqlzoo.net/
  • Refer to Chapter 1, Data Input and Output, about using the cookbook.db database
主站蜘蛛池模板: 陵川县| 郧西县| 莱西市| 南宫市| 崇礼县| 潢川县| 清新县| 新丰县| 邵东县| 绵阳市| 会同县| 平武县| 晋州市| 阿瓦提县| 小金县| 禹城市| 赣榆县| 军事| 曲靖市| 名山县| 梁山县| 北宁市| 韩城市| 敦化市| 碌曲县| 福建省| 改则县| 大姚县| 勃利县| 竹山县| 余庆县| 土默特左旗| 台东县| 瑞金市| 中江县| 大竹县| 米易县| 视频| 望奎县| 阿拉善左旗| 平邑县|