Ask the Expert

Basic three-table join with a condition

I have to join three tables: the Rental table, that has the First and Last Name of the Renter, the Rental Unit table that has the Rental Address, and the Active Rentals table that has the Start Date and Length. The information must come from the Active Rentals table. Only active rentals can be listed. Also I have to display all units with an ocean view. These come from Yes/No fields. I want to use True and False as criterion.

    Requires Free Membership to View

Let's take the active rentals first. You said the Active Rentals table has Start Date and Length. I will assume that Length is in days, and that an "active" rental is one where the Start Date is less than today, and if you added Length days, it would make the duration of the rental go from the Start Date to at least today, or beyond today into the future. The join is the easy part.

select U.RentalAddress
     , R.FirstName
     , R.LastName
     , A.StartDate
     , A.Length
  from ActiveRentals A 
  join RentalUnit U
    on A.UnitID = U.UnitID 
  join Rental R
    on A.RentalID = R.RentalID
 where A.StartDate + Length days
    >= current_date

You will probably have to change this syntax to match your particular database. For example, in Microsoft Access, you would need to parenthesize one of the inner joins and change the WHERE condition to

 where DATEADD("d",Length,StartDate) >= DATE()

For your second question, units with an ocean view,

select RentalAddress
  from RentalUnit 
 where OceanView <> 0

For more on Yes/No fields, see Boolean Columns.

This was first published in January 2003

There are Comments. Add yours.

TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to: