I'm going to ask this question here, since it's connected to my last one. I have a table "users" with columns ssn (key) and others, and a table "donations" with columns user, amount and date. To help you in this example, suppose that we have 3 users, USER1, USER2 and USER3 in the first table, and 3 entries in the second table:
USER1 100.00 2011-02-22
USER1 200.00 2010-02-20
USER2 100.00 2010-02-20
I want to return all the users who wither have never made a donation or have made a donation, but the last one was made more than one year ago (so, USER2 and USER3).
This is my query:
SELECT DISTINCT ssn
FROM users LEFT JOIN donations
ON users.cf = donations.user
WHERE donations.date IS NULL
OR '" . $year_ago . "' > (SELECT MAX(date) FROM donations)
$year_ago contains the current date, moved back of one year.
This query return only USER3, instead that both USER3 and USER2.