Update: Added Schema to the bottom...
I have a table of contracts: tbl_contract
And a table of users associated with the contract: tbl_contract2user
There can be any number of entries in tbl_contract2user
, in which an entry existing means that the relationship exists (along with a pending
column where 1 = pending
and 0 = approved
).
My goal here is to select all contracts where there is 1 (or more) active users within the time frame specified (see below).
The problem I'm having is the ability to sort out these contracts properly. The date range is working fine... For some reason I'm having trouble understanding when the number of users is 1 or more...(vs. 0) and yes - I'll be working with that data set (After the query).
See below for the start of the query...
$result = mysql_query("SELECT tbl_contract.id
FROM tbl_contract
LEFT JOIN tbl_contract2user ON tbl_contract.id = tbl_contract2user.contractID
WHERE tbl_contract2user.pending = 0
AND tbl_contract.startDate <= {$billing['start_time']}
AND tbl_contract.endDate >= {$billing['end_time']}");
Schema:
tbl_contract: id, startDate, endDate, value, name, dateCreated
tbl_contract2user: id, contractID, userID, pending