Function: JOIN

Purpose

Query Joins in Wonderful Relations allow for combining data from multiple queries, similar to SQL joins. This enables structured relationships between different datasets, making it possible to retrieve and display connected information efficiently. Unlike traditional SQL joins, joins in Wonderful Relations are managed through the query builder, ensuring a modular, reusable, and configurable approach.

Parameters

Join Type

Defines how the queries are joined.

Joined Query

The secondary query that will be joined.

Determines whether the join should use predefined Wonderful Relations links.

Join Condition

Custom condition for joining queries.

Temporary Table Approach

Loads joined entities into a temporary table to improve performance.

Custom Table Name

Allows renaming the joined table.

Custom Parent Entity

Overrides the entity of the parent query.

Custom Join Entity

Overrides the entity of the joined query.

Sort Order

Defines the order in which joins are applied.

Join Types

JOIN

Standard inner join, returning only matching rows from both queries.

LEFT JOIN

Returns all rows from the left (main) query, with matching rows from the joined query.

RIGHT JOIN

Returns all rows from the right (joined) query, with matching rows from the main query.

Joined Query

Only queries configured in Wonderful Relations can be joined. If you need to join a MySQL table, you must first create a query for it. Queries cannot be joined with themselves.

When Use Link is enabled, the join will use the predefined Wonderful Relations Link System, which automatically determines relationships between entities. If disabled, a manual Join Condition must be provided.

Join Condition

The Join Condition is required if Use Link is disabled. It defines how records from the two queries should be matched.

Example:

JOIN parent_table.id = joined_table.parent_id

This condition specifies that the id field in the parent table corresponds to the parent_id field in the joined table.

Temporary Table Approach

For large datasets, Temporary Table Approach can be used to load joined entities into a temporary table before performing the join. This can significantly improve performance by reducing the number of direct joins on large tables.

Custom Table Name

To avoid conflicts when joining the same table multiple times, you can specify a Custom Table Name. This ensures that each join has a unique reference within the query.

Custom Parent Entity & Custom Join Entity

By default, joins use the entity structure defined in the main and joined queries. However, in some cases, overriding the parent or joined entity is necessary.

Example use case:

  • If the parent entity does not match the expected relationship, it can be overridden.
  • If a query should return data under a different entity structure, the joined entity can be modified.

Example Queries

Simple Join (LEFT JOIN)

SELECT
  wp_wr_entity.id,
  wp_wr_project.title AS `project_title`
FROM wp_wr_entity
LEFT JOIN wp_wr_project ON wp_wr_entity.project = wp_wr_project.id

SELECT
  wp_wr_datatable_column.id,
  wp_wr_datatable.title
FROM wp_wr_datatable_column
JOIN wp_wr_link link_1_2_3_l ON  
  (link_1_2_3_l_l.entity2 = parent_entity
   AND link_1_2_3_l.id2 = wp_wr_datatable_column.id
   AND link_1_2_3_l.entity1 = child_entity)
JOIN (
 SELECT 
    wp_wr_datatable.id,
    wp_wr_datatable.title
  FROM 
    wp_wr_datatable
    ) wp_wr_datatable ON 
 (link_1_2_3_l_l.entity2 = parent_entity AND link_1_2_3_l_l.id2 = wp_wr_datatable_column.id AND link_1_2_3_l_l.id1 = wp_wr_datatable.id )

Conclusion

Query Joins in Wonderful Relations provide a structured and flexible way to link data from multiple queries. By leveraging the Use Link feature, Temporary Tables, and Custom Entities, complex relationships can be handled efficiently without writing raw SQL joins. This approach ensures a scalable and maintainable system for managing interconnected datasets. 🚀