Skip to content

Instantly share code, notes, and snippets.

@skorfmann
Created June 19, 2010 12:36
Show Gist options
  • Save skorfmann/444861 to your computer and use it in GitHub Desktop.
Save skorfmann/444861 to your computer and use it in GitHub Desktop.
/*Selects all contracts, which aren't billed yet*/
SET @start_date = '2010-06-01', @end_date = '2010-06-30';
SET @month = MONTH(CURDATE()), @year = YEAR(CURDATE());
SELECT c.id, c.start_date, c.end_date, IF(tmp.id IS NULL, 'Billable', 'Not Billable')
FROM contracts c
LEFT JOIN (
SELECT r.id, r.contract_id
FROM receivables r LEFT JOIN billing_cycles b ON r.billing_cycle_id = b.id
WHERE b.month = @month AND b.year = @year
) tmp ON c.id = tmp.contract_id
WHERE (@start_date BETWEEN c.start_date AND c.end_date) OR
(c.start_date BETWEEN @start_date AND @end_date);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment