%% 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).

head(data,3)

%
% Close the database connection.

close(conn)

% Copyright 2020 The MathWorks, Inc.