The source, joined table used with the Edit - Join dialog can be a query, but the Join dialog also can also use a query as the target, original table, to be used to copy data into the query, if the result table is writable.
This allows joining data to, for example, a table alias, such as TABLE [datasource]::[table] or to a selection in an existing component, such as TABLE CALL Selection([component], true) .
This works with queries that have been saved as a query component in the project pane, and not to queries that exist only within a Command Window, which have no persistent name and, as such, cannot be addressed from an update query.
Pending updates: The former Save update query check box in the Join dialog has been replaced with a new Setup Join and Edit Query button, which provides slightly different, but more convenient, operation. The Join Component button has been renamed Join. Illustrations and commentary in this topic will be updated soon.
To follow along with table examples in this topic, download from the Examples page on the Manifold website the books.mxb project. That project contains the publishers and titles tables used in examples.
See the video version of this example in the Join Dialog Part 4 - Joins into Queries video.
We will create a query and then copy data from the publishers table into the query using the Join dialog. We will write the query so it steers the joined data only into selected records.
We start with the titles table as seen above, again with NULLs in the publisher field. We have selected some records in the table.
In the Project pane, we right-click and choose Create - New Query. We create a new query called titles selection.
We double-click the titles selection query to open it in a Command Window, and then we enter the SQL query
TABLE CALL Selection([titles], true);
When we enter the SQL text it is automatically stored in the query. We run the query by pressing the ! Run button in the main toolbar to get the results table shown above. The query uses the Selection( ) SQL function to generate a results table of selected records.
With the focus on the open titles selection window, we choose Edit - Join.
The Join dialog launches with the titles selection query automatically loaded as the original table. We choose the pub_id field for the key field.
We choose the publishers table as the joined table, with the pub_id field automatically being loaded as the key field.
The Add button is not enabled, because we cannot add new fields to a query: the only fields available to us are those in the SELECT list of the query. We can only write to fields that are exposed as writable in the results table of the query. That includes the publishers field we want to update with joined data.
We double-click into the joined table cell for the publishers field.
From the pull down list of fields from the publishers table, we choose the pub_name field. We check Save update query and press Join Component.
Like magic, the publisher field in the titles table is updated with publisher names from the pub_name field in the publishers table, but only for selected records.
If we want to see what SQL query Manifold used to do the update, we can double-click open the query that the Join dialog saved, the titles selection Update:
t.[title_id] AS tkey0,
t.[publisher] AS t0,
s.[pub_name] AS s0
FROM [titles selection] AS t LEFT JOIN [publishers] AS s ON t.[pub_id] = s.[pub_id]
t0 = s0;
The text shows a another compact, tight example of how to do a join update through a query.
Terminology - The original table also may be called the target table, and the table providing data also may be called the source table.
Quick reference - With the Join dialog open, press F1 to open a quick reference guide in a browser window.
Guessing the key field - When we launch the dialog with an original field and when we choose a joined field, the dialog will try to guess what fields we might want to use as key fields and will load those first by default. If it guesses wrong, we can choose the field we want. For the original table, the dialog tries to use a field (other than mfd_id) that has a BTREE / BTREENULL index, with a data type preference for numbers, then text, then everything else, and a name preference for field names with id in them, such as ... id or ..._id, then ...id and then everything else. For the joined table, the dialog uses similar logic, but first tries to use a field with the same name as in the original table.
Added fields are static - Added fields are static, that is, if data in the joined table changes the fields in the original table that received data in the Join operation from the joined table will not automatically be updated. We can easily update the table any time we want using a saved update query.
Spatial joins are automatically parallel - Joining data between drawings uses parallel GeomOverlay function variants. Joins from images to drawings are parallelized through a nested SELECT using THREADS.
Why only in the same data source - The Join dialog works only with tables and queries within the same data source to keep the dialog simple and fast. An important part of keeping the dialog simple is to show only those tables or queries in the pull down list for the joined table that can be used in a join. Scanning all tables and queries in the same data source is reasonable, but if there are many data sources or many levels of nesting (data sources within data sources) there could be hundreds of thousands, if not millions, of tables and queries within the hierarchy to scan. For people working in map projects without such elaborate data sources, most of the time what they do will be within the .map project data source so everything works by default. People who link in data sources can quickly add a simple aliasing query, as illustrated in this topic, to use tables and queries from other data sources. This method also makes it possible for the same Join dialog to work within other data sources, when aliasing queries refer back either to the .map project data source or to other data sources.
Buy and read a Fehily book on SQL - Manifold recommends Chris Fehily's excellent books on SQL. Chris makes learning SQL really easy. To encourage Manifold readers to get a Fehily book and to read it, Manifold examples are often integrated with examples in the Fehily SQL books, such as the tables used in this topic.
Example data - To follow along with table examples in this topic, download from the Examples page on the Manifold website the books.mxb project. That project contains the publishers and titles tables used in examples.
See the Join Videos list of videos showing how to use the Join dialog.
Editing and Combining Data
Editable Results Tables