I have this table…
--------------------------------------
| user_id | status | status_date |
--------------------------------------
| 1 | Current | 2012-08-01 |
| 1 | Referral | 2012-03-14 |
| 2 | Referral | 2012-04-23 |
| | | |
--------------------------------------
How would I query to find a distinct user_id who has a referral date before 2012-06-30 AND either a current date of after 2012-06-30 or no current status record at all?
Database is MySQL.
You can do this using a LEFT JOIN:
Or, using
GROUP BYwithHAVINGandCOUNT(CASE ...)It will depend on your indexes and amount of data as to which performs better, I’d imagine in most cases it will be the former