%% Specify Key for Joining Two Database Tables
% Use a MySQL® native interface database connection to import joined product data from two MySQL database tables into MATLAB®. Specify the key to use for joining the tables.
%
% 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. 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)

%
% Close the database connection.

close(conn)

% Copyright 2020 The MathWorks, Inc.