0
我有這3和表(這些結構):MySQL查詢選擇與條件及加入
outreach
id url profile_id
------------------------------------------
40 www.google.com 2
41 www.yahoo.com 3
42 www.test.com 1
outreach_links
id outreach_id end_date status
-----------------------------------------------
1 41 2016-01-12 Pending
2 40 2016-03-12 Pending
3 40 2016-02-12 Approved
comments
id outreach_id name
----------------------------
1 40
2 40
3 40
,我有這個疑問:
select o.*,
SUM(if(ol.status = "Approved" and (ol.end_date > now() or end_date is null), 1, 0)) as cond1,
SUM(if(ol.status = "Pending" and (ol.end_date != now() or end_date is null), 1, 0)) as cond2,
SUM(if(ol.status = "Pending" and (ol.end_date < now()), 1, 0)) 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
我試圖解決這一問題的查詢,並它還選擇以下內容:
1). ol.* ONLY if MAX(end_date) and
2). Count(id.comment) count all comments for that particular row
是否有可能?
現在這裏是輸出
+"id": "40"
+"profile_id": "2"
+"url": "http://www.google.com"
+"created_at": "2016-12-05 21:55:10"
+"updated_at": "2016-12-05 22:49:56"
+"cond1": "0"
+"cond2": "0"
+"cond3": "5"
我想補充
+"max_date": get me max of end_date and the whole row of the row highlighted
+"Count(comments)": get me all the comments count for this one which is 3
感謝
有人幫我在這裏,這將解釋邏輯COND1,COND2和3 >> http://stackoverflow.com/questions/41113660/multiple-not-exsits-in-mysql-to-check-count-of -rows檢查出來,但我需要擴展它來選擇最大end_date和評論計數 – user3150060