I have two tables
1) outreach
id profile_id url
-------------------------
1 2 www.test.com
2 3 www.google.com
3 4 www.example.com
2). outreach_links
id outreach_id end_date status
------------------------------------
1 1 2016-12-28 00:00:00 Approved
2 1 2016-12-16 00:00:00 Approved
3 1 NUll Pending
4 1 2016-12-11 00:00:00 Approved
I have this SQL Query with Left Join and Conditions that is working fine except I want to select the whole ROW of the MAX end_date that meets the 3 condition. so in this case the first row with end_date = 2016-12-28 00:00:00
select o.*,ol.*,MAX(ol.end_date) as max_date, SUM(ol.status = "Approved" and (ol.end_date > Now() or end_date is null)) as cond1, SUM(ol.status = "Pending") as cond2,
SUM(ol.status = "Approved" and (ol.end_date < Now() and ol.end_date is not null)) as cond3
FROM outreach o
LEFT JOIN outreach_links ol on ol.outreach_id = o.id
WHERE o.profile_id=2
GROUP BY o.id
HAVING (cond1 = 0 and cond2 = 0) or (cond1 = 0 and (cond2 = 1 and cond3 >=1))
ORDER BY ol.end_date desc
but this is the output for this query ( its picking the pending for some reason) >>
+"id": "3"
+"profile_id": "2"
+"url": "www.test.com"
+"outreach_id": "1"
+"end_date": null
+"status": "Pending"
+"max_date": "2016-12-28 00:00:00"
+"cond1": "0"
+"cond2": "1"
+"cond3": "3"
I want to get this instead
+"id": "1"
+"profile_id": "2"
+"url": "www.test.com"
+"outreach_id": "1"
+"end_date": 2016-12-28 00:00:00
+"status": "Approved"
+"max_date": "2016-12-28 00:00:00"
+"cond1": "0"
+"cond2": "1"
+"cond3": "3"
The first row with MAX end date, how can I do that keeping this same Query ??
Thanks