Database Toolbox |
Creating Subqueries for Values from Multiple Tables
Use the Where feature in Advanced query options to specify a subquery, which further limits a query by using values found in other tables. This is referred to as nested SQL. With the VQB, you can include only one subquery; use Database Toolbox functions to use multiple subqueries.
This example uses basic.qry
(see Creating and Running a Query to Import Data). It retrieves sales volumes for the product whose description is Building Blocks. The table used for basic.qry
, salesVolume
, has sales volumes and a stock number field, but not a product description field. Another table, productTable
, has the product description and stock number, but not the sales volumes. Therefore, the query needs to look at productTable
to get the stock number for the product whose description is Building Blocks, and then has to look at the salesVolume
table to get the sales volume values for that stock number:
basic.qry
. For instructions, see Using a Saved Query.
cellarray
and Read NULL numbers as to NaN
.
productTable
, which contains the association between the stock number and the product description.
stockNumber
.
productDescription
from Fields.
=
.
'Building Blocks'
(include the single quotation marks to denote it is a string).
The clause appears in the Current subquery Where clauses area and updates the SQL subquery statement.
C
.
C
at the prompt in the Command Window to see the results.
dbtoolboxdemo
as the Data source. This clears the VQB selections
made during a previous query.
productTable
from Tables.
stockNumber
and productDescription
from Fields.
P
.
P
at the prompt in the Command Window to view the results.
The results show that item 400345 has the product description Building Blocks.
Creating Queries for Results from Multiple Tables creates a query that includes the product description in the results.
Presenting Results in Specified Order | Creating Queries for Results from Multiple Tables |
© 1994-2005 The MathWorks, Inc.