How can I query a database to extract data from multiple tables?

7 views (last 30 days)
I am using the Database Toolbox to connect to an Access database that has multiple tables. I would like to query the tables to retrieve information from the Database.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Jun 2009
The key to retrieving information from a database is the query that is passed to the driver to retrieve that information. The code below shows an example of connecting to a database, and retrieving data from multiple tables based on some criteria.
query = 'SELECT ALL Table1.Name, Table1.Number FROM Table2, Table1 WHERE Table1.Number = Table2.Number AND Table1.Name = Table2.Name';
conn = database('Example','','');
cursor = exec(conn,query);
results = fetch(cursor);
results.data
Note that you will need to download the attached MDB-file and create a new data source in Access. To set up the data source, follow the steps below:
1) Download the attached MDB file and save it as Example.mdb
2) In your system's Control Panel, open up the Administrative Tools dialog
3) Select the Data Source (ODBC) tool
4) Add a new datasource, called Example, by selecting the Data Source to be the file downloaded in step 1

More Answers (0)

Tags

No tags entered yet.

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!