Access: Multi-table Queries (Part 1) - YouTube

Channel: unknown

[1]
We've already talked about how to create a simple
[9]
query that only uses one table.
[12]
But queries that involve more than one table let you ask much more interesting questions
[16]
to your database.
[17]
Now this also takes a little bit more planning than a single-table query, and there are four
[21]
steps that you can use to help you design your query.
[24]
The first step is Pinpoint exactly what you want to find out.
[28]
In other words, which question are you trying to answer?
[31]
In this example, let's say our bakery is sending out coupons to our customers who live outside
[35]
the city limits, to entice them to come back to our bakery.
[38]
Obviously, we don't want to send them to people who live really far away - we just want to
[43]
focus on people who live relatively close to Raleigh.
[46]
And we're also just going to send them to customers who have previously placed orders
[49]
at our bakery.
[51]
The customers who meet all three of these requirements will receive coupons.
[55]
So the question that we're trying to answer is 'Which customers live in our area, are
[60]
outside the city limits, and have placed an order at our bakery?'
[64]
The second step is to Identify the information that we need.
[68]
We'll need the customers' names and their contact information.
[72]
And in order to know whether they have placed an order at our bakery, we'll also need to
[75]
look at the Order ID numbers.
[78]
The third step is to Locate the tables that contain the information that we need.
[83]
In this case, the customers' names and contact information are stored in the Customers table.
[88]
And the Order ID numbers are stored in the Orders table.
[91]
So that means we need both of these tables in our query.
[97]
At this point, we have enough information to start creating our query.
[101]
Go to the Create tab, and select Query design.
[108]
We're going to add the Customers table and the Orders table.
[115]
And then close this window.
[117]
And you resize these windows if you need to.
[125]
Then we're going to double click the fields that we need.
[128]
I'm adding the customer's first name, last name, street address, city, state, zip code,
[139]
and phone number.
[142]
And from the Orders table, we'll need the ID field.
[146]
When you have more than one table in a query, they will be connected by a line, and this
[150]
is called a Join.
[152]
The join will often have an arrow that points to the left or the right, which tells the
[156]
query which table to look at first.
[159]
Sometimes you'll need to change the direction to get the results that you want.
[163]
We're going to double-click on the join to change it, and we want to select the third
[168]
option which says 'Include ALL records from the Orders Table'.
[172]
And in the next video, we're going to talk a little more about *why* we're choosing this
[175]
option, but basically this means that it will pull from the Orders table first, which ensures
[180]
that only the customers who have placed an order will be included.
[185]
When you click OK, you can see that the arrow now points to the left.
[191]
The fourth step is to determine which search criteria you need to use.
[195]
We're going to be adding criteria under the City and Phone Number fields.
[199]
First, we want to exclude all of the customers who are in Raleigh.
[204]
To do this, we're going to need to use a very specific syntax.
[208]
So for the City criteria, type Not In, and then in parentheses type Raleigh in quotation
[215]
marks.
[216]
You can use this syntax whenever you want to exclude something from the query results.
[221]
Now we also need some way of limiting the results to just the nearby towns.
[225]
And in this case we're going to do this by getting the area code from the Phone Number
[229]
field.
[230]
The 919 area code covers Raleigh and a number of nearby cities and towns, so this should
[235]
give us a pretty good range.
[238]
We'll need to use a syntax that looks at the beginning of each phone number.
[241]
In the Phone Number column, type Like, and in parentheses, type *quote* 919, asterisk,
[249]
*end quote*.
[251]
The asterisk means that any phone number can come after the 919 area code.
[255]
There are many other syntaxes that you can use, and in the next video we'll look at a
[259]
few other examples of these.
[262]
In this case we are putting the criteria on the same row, because we want the customers
[266]
to meet both of these criteria.
[268]
If they just meet one of the criteria, then they're not going to be included in the query
[272]
results.
[273]
If we needed them to meet one or the other, then we would put one of the criteria on the
[277]
next row.
[280]
This query is finished now, so we can Run it to see the results.
[284]
And you can see that each customer meets both criteria.
[288]
They are not from Raleigh, and their phone numbers begin with 919.
[291]
[put boxes around city and area code]
[293]
So generally, more complex queries require more planning.
[296]
But you can make it a lot easier by just following the four steps of Pinpointing exactly what
[301]
you want to find out, Identifying the information that you need, Locating the tables that contain
[307]
the information, and Determining exactly what criteria you need.
[312]
And in the next video, we're going to look at joins and search criteria in a little bit
[316]
more detail.