jetspin
Programmer
- Mar 5, 2002
- 76
I'm having a difficult time writing an SQL statement. Hope I can explain it. (Or after reading this, should I just set a 'status' flag and avoid a complicated query?
)
In my db we lease phone numbers.
There is a phone number table and an order table.
The phone number table has a one-many relationship to the order table. Phone number recs can exist without an order rec. When a phone number is leased, an order rec is created with a blank ordReturnDate. When the the phone number is returned, the ordReturnDate is filled in.
When the same phone number is leased again, a new order rec is created and the cycle continues. So a phone number rec can exist without any orders if it has never been leased and a phone number can have many orders if it has been leased. And (theoritically) only one of the orders can have a blank ordReturndate at a time. (In other words, a phone number can only be leased one order at a time.)
I want to write a query for when a new order is created that will list only available phone numbers to select from.
This would be (all phone numbers that have no orders at all) or (if the phone number has any orders there can be no orders with null ordReturnDate).
"Phone Numbers"
tblPhoneNumbers
pnrPnrID (PK)
pnrPhoneNumber
"Orders"
tblOrders
ordOrdID (PK)
ordPnrID (FK from pnrPnrID)
ordReturnDate
Thanks. Any help at all would be appreciated.
In my db we lease phone numbers.
There is a phone number table and an order table.
The phone number table has a one-many relationship to the order table. Phone number recs can exist without an order rec. When a phone number is leased, an order rec is created with a blank ordReturnDate. When the the phone number is returned, the ordReturnDate is filled in.
When the same phone number is leased again, a new order rec is created and the cycle continues. So a phone number rec can exist without any orders if it has never been leased and a phone number can have many orders if it has been leased. And (theoritically) only one of the orders can have a blank ordReturndate at a time. (In other words, a phone number can only be leased one order at a time.)
I want to write a query for when a new order is created that will list only available phone numbers to select from.
This would be (all phone numbers that have no orders at all) or (if the phone number has any orders there can be no orders with null ordReturnDate).
"Phone Numbers"
tblPhoneNumbers
pnrPnrID (PK)
pnrPhoneNumber
"Orders"
tblOrders
ordOrdID (PK)
ordPnrID (FK from pnrPnrID)
ordReturnDate
Thanks. Any help at all would be appreciated.