Inner join between two PostgreSQL database tables
returns a table resulting from an inner join between the left and right database tables.
This function matches rows using all shared columns, or keys, in both database tables. The
inner join retains only the rows that match between the two tables. Executing this function
is the equivalent of writing the SQL statement data
= sqlinnerjoin(conn
,lefttable
,righttable
)SELECT * FROM lefttable,righttable
INNER JOIN lefttable.key = righttable.key
.
uses additional options specified by one or more name-value pair arguments. For example,
data
= sqlinnerjoin(conn
,lefttable
,righttable
,Name,Value
)'Keys','productNumber'
specifies using the
productNumber
column as a key for joining the two database
tables.
Use a PostgreSQL native interface database connection to import product data from an inner join between two PostgreSQL database tables into MATLAB®.
Create a PostgreSQL native interface database connection to a PostgreSQL database using the data source name, user name, and password. The database contains the tables productTable
and suppliers
.
datasource = "PostgreSQLDataSource"; username = "dbdev"; password = "matlab"; conn = postgresql(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 sqlinnerjoin
function automatically detects the shared column between the tables. data
is a table that contains the matched rows from the two tables.
lefttable = "productTable"; righttable = "suppliers"; data = sqlinnerjoin(conn,lefttable,righttable);
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
).
head(data,3)
ans=3×10 table
productnumber stocknumber suppliernumber unitcost productdescription suppliernumber_1 suppliername city country faxnumber
_____________ ___________ ______________ ________ __________________ ________________ _________________ __________ ________________ ______________
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"
3 4.01e+05 1009 17 "Slinky" 1009 "Doll's Galore" "London" "United Kingdom" "44 222 2397"
Close the database connection.
close(conn)
Use a PostgreSQL native interface database connection to import joined product data from two PostgreSQL database tables into MATLAB®. Specify the key to use for joining the tables.
Create a PostgreSQL native interface database connection to a PostgreSQL database using the data source name, user name, and password. The database contains the tables productTable
and suppliers
.
datasource = "PostgreSQLDataSource"; username = "dbdev"; password = "matlab"; conn = postgresql(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. Specify the key, or shared column, between the tables using the 'Keys'
name-value pair argument. data
is a table that contains the matched rows from the two tables.
lefttable = "productTable"; righttable = "suppliers"; data = sqlinnerjoin(conn,lefttable,righttable,'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
).
head(data,3)
ans=3×10 table
productnumber stocknumber suppliernumber unitcost productdescription suppliernumber_1 suppliername city country faxnumber
_____________ ___________ ______________ ________ __________________ ________________ _________________ __________ ________________ ______________
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"
3 4.01e+05 1009 17 "Slinky" 1009 "Doll's Galore" "London" "United Kingdom" "44 222 2397"
Close the database connection.
close(conn)
conn
— PostgreSQL native interface database connectionconnection
objectPostgreSQL native interface database connection, specified as a connection
object.
lefttable
— Left tableLeft 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 tableRight 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
Specify optional
comma-separated pairs of Name,Value
arguments. Name
is
the argument name and Value
is the corresponding value.
Name
must appear inside quotes. You can specify several name and value
pair arguments in any order as
Name1,Value1,...,NameN,ValueN
.
data =
sqlinnerjoin(conn,"productTable","suppliers",'LeftCatalog',"toystore_doc",'LeftSchema',"dbo",'RightCatalog',"toy_shop",'RightSchema',"toys",'MaxRows',5)
performs an inner join between left and right tables by specifying the catalog and schema
for both tables and returns five matched rows.'LeftCatalog'
— Left catalogLeft 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 catalogRight 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
'LeftSchema'
— Left schemaLeft schema, specified as the comma-separated pair consisting
of 'LeftSchema'
and a character
vector or string scalar. Specify the database schema
name where the left table of the join is
stored.
Example: 'LeftSchema','dbo'
Data Types: char
| string
'RightSchema'
— Right schemaRight schema, specified as the comma-separated pair consisting of
'RightSchema'
and a character vector or string scalar. Specify
the database schema name where the right table of the join is stored.
Example: 'RightSchema','dbo'
Data Types: char
| string
'Keys'
— KeysKeys, 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 keysLeft 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. The
sqlinnerjoin
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 keysRight 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 sqlinnerjoin
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 returnMaximum number of rows to return, specified as the comma-separated pair consisting of
'MaxRows'
and a positive numeric scalar. By default, the
sqlinnerjoin
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:
"preserve"
— Preserve most variable names when the
sqlinnerjoin
function imports data. For details, see
the Limitations section.
"modify"
— Remove non-ASCII characters from variable names when the sqlinnerjoin
function imports data.
Example: 'VariableNamingRule',"modify"
Data Types: string
data
— Joined dataJoined data, returned as a table that contains the matched rows from the join of the
left and right tables. data
also contains a variable for each column
in the left and right tables.
If the column names are shared between the joined database tables and have the same
case, then the sqlinnerjoin
function adds a unique suffix to the
corresponding variable names in data
.
When you import data, the sqlinnerjoin
function converts the
data type of each column from the PostgreSQL database to the MATLAB data type. This table maps the data type of a database column to the
converted MATLAB data type.
PostgreSQL Data Type | MATLAB Data Type |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
The name-value pair argument 'VariableNamingRule'
has these limitations
when it is set to the value 'modify'
.
The variable names Properties
, RowNames
, and
VariableNames
are reserved identifiers for the
table
data type.
The length of each variable name must be less than the number returned by namelengthmax
.
close
| postgresql
| sqlfind
| sqlouterjoin
| sqlread
You have a modified version of this example. Do you want to open this example with your edits?