Beginning SQL

Beginning SQL Beginning SQL

marjan.fesb.hr
from marjan.fesb.hr More from this publisher
20.07.2013 Views

Appendix A 382 You should use the following SQL if you’re using MS Access: SELECT C1.Category, MD1.FirstName, MD1.LastName, MD2.FirstName, MD2.LastName FROM (((FavCategory AS FC1 INNER JOIN FavCategory AS FC2 ON FC1.CategoryId = FC2.CategoryId) INNER JOIN Category AS C1 ON C1.CategoryId = FC1.CategoryId) INNER JOIN MemberDetails MD1 ON MD1.MemberId = FC1.MemberId) INNER JOIN MemberDetails MD2 ON MD2.MemberId = FC2.MemberId WHERE FC1.MemberId < FC2.MemberId; It might look horrendously long, but broken down into its parts it’s not that bad. The very first step is to pair up people with the same favorite category. To get pairs of people in the same row, you need to use a self-join of the FavCategory table: SELECT FC1.CategoryId, FC1.MemberId, FC2.MemberId FROM FavCategory FC1 INNER JOIN FavCategory FC2 ON FC1.CategoryId = FC2.CategoryId WHERE FC1.MemberId < FC2.MemberId; The WHERE clause is there to prevent the same person from being listed as a pair of themselves on the same row. This gives you the CategoryId and MemberIds involved, but the question requires the category name and the first and last names of the people. First, get the category title from the Category table by joining it to the current results: SELECT C1.Category, FC1.MemberId, FC2.MemberId FROM FavCategory FC1 INNER JOIN FavCategory FC2 ON FC1.CategoryId = FC2.CategoryId INNER JOIN Category C1 ON C1.CategoryId = FC1.CategoryId WHERE FC1.MemberId < FC2.MemberId; Now you need to get the first and last names. For this, you need to join the MemberDetails table to the current results. However, on the same row are two different MemberIds: for FC1 and for FC2. You can’t just join MemberDetails once; you need two copies of the table joined. The first copy provides the first and last names for FC1’s MemberId, and the second copy provides the lookup for the first and last names from FC2’s MemberId: SELECT C1.Category, MD1.FirstName, MD1.LastName, MD2.FirstName, MD2.LastName FROM FavCategory FC1 INNER JOIN FavCategory FC2 ON FC1.CategoryId = FC2.CategoryId INNER JOIN Category C1 ON C1.CategoryId = FC1.CategoryId INNER JOIN MemberDetails MD1 ON MD1.MemberId = FC1.MemberId INNER JOIN MemberDetails MD2 ON MD2.MemberId = FC2.MemberId WHERE FC1.MemberId < FC2.MemberId;

Appendix A<br />

382<br />

You should use the following <strong>SQL</strong> if you’re using MS Access:<br />

SELECT C1.Category, MD1.FirstName, MD1.LastName, MD2.FirstName, MD2.LastName<br />

FROM (((FavCategory AS FC1 INNER JOIN FavCategory AS FC2<br />

ON FC1.CategoryId = FC2.CategoryId)<br />

INNER JOIN Category AS C1 ON C1.CategoryId = FC1.CategoryId)<br />

INNER JOIN MemberDetails MD1 ON MD1.MemberId = FC1.MemberId)<br />

INNER JOIN MemberDetails MD2 ON MD2.MemberId = FC2.MemberId<br />

WHERE FC1.MemberId < FC2.MemberId;<br />

It might look horrendously long, but broken down into its parts it’s not that bad. The very first step is to<br />

pair up people with the same favorite category. To get pairs of people in the same row, you need to use a<br />

self-join of the FavCategory table:<br />

SELECT FC1.CategoryId, FC1.MemberId, FC2.MemberId<br />

FROM FavCategory FC1 INNER JOIN FavCategory FC2<br />

ON FC1.CategoryId = FC2.CategoryId<br />

WHERE FC1.MemberId < FC2.MemberId;<br />

The WHERE clause is there to prevent the same person from being listed as a pair of themselves on the<br />

same row. This gives you the CategoryId and MemberIds involved, but the question requires the category<br />

name and the first and last names of the people. First, get the category title from the Category table<br />

by joining it to the current results:<br />

SELECT C1.Category, FC1.MemberId, FC2.MemberId<br />

FROM FavCategory FC1 INNER JOIN FavCategory FC2<br />

ON FC1.CategoryId = FC2.CategoryId<br />

INNER JOIN Category C1 ON C1.CategoryId = FC1.CategoryId<br />

WHERE FC1.MemberId < FC2.MemberId;<br />

Now you need to get the first and last names. For this, you need to join the MemberDetails table to the<br />

current results. However, on the same row are two different MemberIds: for FC1 and for FC2. You can’t<br />

just join MemberDetails once; you need two copies of the table joined. The first copy provides the first<br />

and last names for FC1’s MemberId, and the second copy provides the lookup for the first and last<br />

names from FC2’s MemberId:<br />

SELECT C1.Category, MD1.FirstName, MD1.LastName, MD2.FirstName, MD2.LastName<br />

FROM FavCategory FC1 INNER JOIN FavCategory FC2<br />

ON FC1.CategoryId = FC2.CategoryId<br />

INNER JOIN Category C1 ON C1.CategoryId = FC1.CategoryId<br />

INNER JOIN MemberDetails MD1 ON MD1.MemberId = FC1.MemberId<br />

INNER JOIN MemberDetails MD2 ON MD2.MemberId = FC2.MemberId<br />

WHERE FC1.MemberId < FC2.MemberId;

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!