sqlouterjoin - Outer join between two MySQL database tables - MATLAB (original) (raw)

Outer join between two MySQL database tables

Since R2020b

Syntax

Description

[data](#mw%5F9e81d6fa-d41b-42c8-b45d-b81d0b725da2) = sqlouterjoin([conn](#mw%5F542232d6-0055-4b44-9346-c1099b9a7dfc%5Fsep%5Fmw%5Fe8a1bdf2-6db9-421c-be5b-85d2f5c29cb1),[lefttable](#d126e79842),[righttable](#d126e79871),'Type',[type](#mw%5Fc8c31df8-0e8d-4716-a911-1a57e96df689)) returns a table resulting from an outer join between the left and right database tables. Specify the join type to be a left or right join. This function matches rows using all shared columns, or keys, in both database tables. The outer join retains the matched and unmatched rows between the two tables. Executing this function is the equivalent of writing the SQL statement SELECT * FROM lefttable LEFT JOIN righttable ON lefttable.key = righttable.key or SELECT * FROM lefttable RIGHT JOIN righttable ON lefttable.key = righttable.key.

example

[data](#mw%5F9e81d6fa-d41b-42c8-b45d-b81d0b725da2) = sqlouterjoin([conn](#mw%5F542232d6-0055-4b44-9346-c1099b9a7dfc%5Fsep%5Fmw%5Fe8a1bdf2-6db9-421c-be5b-85d2f5c29cb1),[lefttable](#d126e79842),[righttable](#d126e79871),[Name,Value](#namevaluepairarguments)) uses additional options specified by one or more name-value arguments. For example, specifyKeys = "productNumber" to use the productNumber column as a key for joining the two database tables.

example

Examples

collapse all

Join Two Database Tables Using MySQL Native Interface

Use a MySQL® native interface database connection to import product data from an outer join between two MySQL database tables into MATLAB®.

Create a MySQL native interface database connection to a MySQL database using the data source name, user name, and password. The database contains the tables productTable and suppliers.

datasource = "MySQLNative"; username = "root"; password = "matlab"; conn = mysql(datasource,username,password);

Join two database tables, productTable and suppliers. The productTable table is the left table of the join, and the suppliers table is the right table of the join. The sqlouterjoin function automatically detects the shared column between the tables. Use the 'Type' name-value pair argument to retrieve records that have matching values in the selected column of both tables, and unmatched records from the left table only. data is a table that contains the matched and unmatched rows from the two tables.

lefttable = "productTable"; righttable = "suppliers"; data = sqlouterjoin(conn,lefttable,righttable,'Type',"left");

Display the first three rows of joined data. The columns from the right table (suppliers) appear to the right of the columns from the left table (productTable).

ans=3×10 table productNumber stockNumber supplierNumber unitCost productDescription SupplierNumber SupplierName City Country FaxNumber
_____________ ___________ ______________ ________ __________________ ______________ _________________ __________ ________________ ______________

      8          2.1257e+05          1001             5       "Train Set"                1001         "Wonder Products"    "New York"    "United States"     "212 435 1617"
      1          4.0035e+05          1001            14       "Building Blocks"          1001         "Wonder Products"    "New York"    "United States"     "212 435 1617"
      2          4.0031e+05          1002             9       "Painting Set"             1002         "Terrific Toys"      "London"      "United Kingdom"    "44 456 9345" 

Close the database connection.

Specify Key for Joining Two Database Tables

Use a MySQL® native interface database connection to import product data from an outer join between two MySQL database tables into MATLAB®.

Create a MySQL native interface database connection to a MySQL database using the data source name, user name, and password. The database contains the tables productTable and suppliers.

datasource = "MySQLDataSource"; username = "root"; password = "matlab"; conn = mysql(datasource,username,password);

Join two database tables, productTable and suppliers. The productTable table is the left table of the join, and the suppliers table is the right table of the join. The sqlouterjoin function automatically detects the shared column between the tables. Use the 'Type' name-value pair argument to retrieve records that have matching values in the selected column of both tables, and unmatched records from the left table only. Specify the key, or shared column, between the tables using the 'Keys' name-value pair argument. data is a table that contains the matched and unmatched rows from the two tables.

lefttable = "productTable"; righttable = "suppliers"; data = sqlouterjoin(conn,lefttable,righttable,'Type',"left",'Keys',"supplierNumber");

Display the first three rows of matched data. The columns from the right table (suppliers) appear to the right of the columns from the left table (productTable).

ans=3×10 table productNumber stockNumber supplierNumber unitCost productDescription SupplierNumber SupplierName City Country FaxNumber
_____________ ___________ ______________ ________ __________________ ______________ _________________ __________ ________________ ______________

      8          2.1257e+05          1001             5       "Train Set"                1001         "Wonder Products"    "New York"    "United States"     "212 435 1617"
      1          4.0035e+05          1001            14       "Building Blocks"          1001         "Wonder Products"    "New York"    "United States"     "212 435 1617"
      2          4.0031e+05          1002             9       "Painting Set"             1002         "Terrific Toys"      "London"      "United Kingdom"    "44 456 9345" 

Close the database connection.

Filter Rows in Joined Data

Use a MySQL® native interface database connection to import product data from an outer join between two MySQL database tables into MATLAB®. Specify the row filter condition to use for joining the tables.

Create a MySQL native interface database connection to a MySQL database using the data source name, username, and password. The database contains the tables productTable and suppliers.

datasource = "MySQLDataSource"; username = "root"; password = "matlab"; conn = mysql(datasource,username,password);

Join the two database tables, productTable and suppliers. The productTable table is the left table of the join, and the suppliers table is the right table of the join. The sqlouterjoin function automatically detects the shared column between the tables. Use Type to retrieve records that have matching values in the selected column of both tables, and unmatched records from the left table only. The table data contains the matched and unmatched rows from the two tables.

lefttable = "productTable"; righttable = "suppliers"; data = sqlouterjoin(conn,lefttable,righttable,"Type","left");

Display the first five rows of matched data. The columns from the right table appear to the right of the columns from the left table.

productNumber    stockNumber    supplierNumber    unitCost    productDescription    SupplierNumber      SupplierName          City           Country           FaxNumber   
_____________    ___________    ______________    ________    __________________    ______________    _________________    __________    ________________    ______________

      1          4.0034e+05          1001            14       "Building Blocks"          1001         "Wonder Products"    "New York"    "United States"     "212 435 1617"
      1          4.0034e+05          1001            14       "Building Blocks"          1001         "Wonder Products"    "New York"    "United States"     "212 435 1617"
      2          4.0031e+05          1002             9       "Painting Set"             1002         "Terrific Toys"      "London"      "United Kingdom"    "44 456 9345" 
      2          4.0031e+05          1002             9       "Painting Set"             1002         "Terrific Toys"      "London"      "United Kingdom"    "44 456 9345" 
      3            4.01e+05          1009            17       "Slinky"                   1009         "Doll's Galore"      "London"      "United Kingdom"    "44 222 2397" 

Join the same tables, but this time use a row filter. The filter condition is that unitCost must be less than 10. Again, display the first five rows of matched data.

rf = rowfilter("unitCost"); rf = rf.unitCost <= 10; data = sqlouterjoin(conn,lefttable,righttable, ... "Type","left", ... "RowFilter",rf); head(data,5)

productNumber    stockNumber    supplierNumber    unitCost    productDescription    SupplierNumber         SupplierName           City          Country           FaxNumber   
_____________    ___________    ______________    ________    __________________    ______________    ______________________    ________    ________________    ______________

      2          4.0031e+05          1002            9          "Painting Set"           1002         "Terrific Toys"           "London"    "United Kingdom"    "44 456 9345" 
      2          4.0031e+05          1002            9          "Painting Set"           1002         "Terrific Toys"           "London"    "United Kingdom"    "44 456 9345" 
      5          4.0046e+05          1005            3          "Tin Soldier"            1005         "Custers Tin Soldiers"    "Boston"    "United States"     "617 939 1234"
      5          4.0046e+05          1005            3          "Tin Soldier"            1005         "Custers Tin Soldiers"    "Boston"    "United States"     "617 939 1234"
      6          4.0088e+05          1004            8          "Sail Boat"              1004         "Incredible Machines"     "Dublin"    "Ireland"           "01 222 3456" 

Close the database connection.

Input Arguments

collapse all

conn — MySQL® native interface database connection

connection object

MySQL native interface database connection, specified as a connection object. Starting in R2024a, it is recommended that you use setSecret and getSecret to store and retrieve your credentials for databases that require authentication. For more details, refer to this example.

lefttable — Left table

character vector | string scalar

Left table, specified as a character vector or string scalar. Specify the name of the database table on the left side of the join.

Example: 'inventoryTable'

Data Types: char | string

righttable — Right table

character vector | string scalar

Right table, specified as a character vector or string scalar. Specify the name of the database table on the right side of the join.

Example: 'productTable'

Data Types: char | string

type — Outer join type

'left' | 'right'

Outer join type, specified as the comma-separated pair consisting of'Type' and one of these values:

You can specify this value as a character vector or string scalar.

Example: 'Type','left'

Name-Value Arguments

Specify optional pairs of arguments asName1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: data = sqlouterjoin(conn,"productTable","suppliers",'Type','left','MaxRows',5) performs an outer left join between left and right tables and returns five rows of the joined data.

LeftCatalog — Left catalog

character vector | string scalar

Left catalog, specified as the comma-separated pair consisting of 'LeftCatalog' and a character vector or string scalar. Specify the database catalog name where the left table of the join is stored.

Example: 'LeftCatalog','toy_store'

Data Types: char | string

RightCatalog — Right catalog

character vector | string scalar

Right catalog, specified as the comma-separated pair consisting of'RightCatalog' and a character vector or string scalar. Specify the database catalog name where the right table of the join is stored.

Example: 'RightCatalog','toy_store'

Data Types: char | string

Keys — Keys

character vector | string scalar | cell array of character vectors | string array

Keys, specified as the comma-separated pair consisting of 'Keys' and a character vector, string scalar, cell array of character vectors, or string array. Specify a character vector or string scalar to indicate one key. For multiple keys, specify a cell array of character vectors or a string array. Use this name-value pair argument to identify the shared keys (columns) between the two tables to join.

You cannot use this name-value pair argument with the 'LeftKeys' and 'RightKeys' name-value pair arguments.

Example: 'Keys','MANAGER_ID'

Data Types: char | string | cell

LeftKeys — Left keys

character vector | string scalar | cell array of character vectors | string array

Left keys, specified as the comma-separated pair consisting of 'LeftKeys' and a character vector, string scalar, cell array of character vectors, or string array. Specify a character vector or string scalar to indicate one key. For multiple keys, specify a cell array of character vectors or a string array. This name-value pair argument identifies the keys in the left table for the join to the right table.

Use this name-value pair argument with the 'RightKeys' name-value pair argument. Both arguments must specify the same number of keys. Thesqlouterjoin function pairs the values of the keys based on their order.

Example: 'LeftKeys',["productNumber" "Price"],'RightKeys',["productNumber" "Price"]

Data Types: char | string | cell

RightKeys — Right keys

character vector | string scalar | cell array of character vectors | string array

Right keys, specified as the comma-separated pair consisting of 'RightKeys' and a character vector, string scalar, cell array of character vectors, or string array. Specify a character vector or string scalar to indicate one key. For multiple keys, specify a cell array of character vectors or a string array. This name-value pair argument identifies the keys in the right table for the join to the left table.

Use this name-value pair argument with the 'LeftKeys' name-value pair argument. Both arguments must specify the same number of keys. The sqlouterjoin function pairs the values of the keys based on their order.

Example: 'LeftKeys',["productIdentifier" "Cost"],'RightKeys',["productNumber" "Price"]

Data Types: char | string | cell

MaxRows — Maximum number of rows to return

positive numeric scalar

Maximum number of rows to return, specified as the comma-separated pair consisting of'MaxRows' and a positive numeric scalar. By default, thesqlouterjoin function returns all rows from the executed SQL query. Use this name-value pair argument to limit the number of rows imported into MATLAB®.

Example: 'MaxRows',10

Data Types: double

VariableNamingRule — Variable naming rule

"preserve" (default) | "modify"

Variable naming rule, specified as the comma-separated pair consisting of 'VariableNamingRule' and one of these values:

Example: 'VariableNamingRule',"modify"

Data Types: string

RowFilter — Row filter condition

<unconstrained> (default) | matlab.io.RowFilter object

Row filter condition, specified as a matlab.io.RowFilter object.

Example: rf = rowfilter("productnumber"); rf = rf.productnumber <= 5; sqlouterjoin(conn,lefttable,righttable,"RowFilter",rf)

Output Arguments

collapse all

data — Joined data

table

Joined data, returned as a table that contains rows matched by keys in the left and right database tables and the retained unmatched rows. data also contains a variable for each column in the left and right tables.

When you import data, the sqlouterjoin function converts the data type of each column from the MySQL database to the MATLAB data type. This table maps the data type of a database column to the converted MATLAB data type.

MySQL Data Type MATLAB Data Type
BIT logical
TINYINT double
SMALLINT double
BIGINT double
REAL double
DOUBLE double
DECIMAL double
NUMERIC double
CHAR string
VARCHAR string
LONGVARCHAR string
TIMESTAMP datetime
DATE datetime
TIME duration
YEAR double
ENUM categorical
JSON char

If the column names are shared between the joined database tables and have the same case, then the sqlouterjoin function adds a unique suffix to the corresponding variable names in data.

The variables in data that correspond to columns in the left table contain NULL values when no matched rows exist in the right database table. Similarly, the variables that correspond to columns in the right table contain NULL values when no matched rows exist in the left database table.

Limitations

The name-value argument VariableNamingRule has these limitations if it is set to the value "modify".

Version History

Introduced in R2020b

expand all

R2023a: Selectively join data based on filter condition

You can use the RowFilter when joining data from database tables.