mysql - Joining 3 tables and count different fields -
i have query in loop through itemlist(alerts), these alerts have replies want know how many replies alert/item has, these alerts/items have sort of like(interaction) button want know how many people liked it, have query gives me how many replies have, don't know how can count how many interactions has... take @ , maybe me ?
this query far:
select a.title, a.lat, a.lon, a.alert_content_id, a.date_added, count(*) `alerts` left join `reply` r on r.alert_id = a.alerts left join `interactions` on i.alert_id = a.alerts group a.title, a.lat, a.lon, a.alert_content_id, a.date_added
now count returns number of replies, how can count number of interactions well? desired result
|a.title|a.lat|a.lon|a.alert_content_id|a.date_added|count(replies)|count(interactions)|
count replies number of rows a.alerts == r.alert_id
, count interactions number of rows a.alerts == i.alert_id
so after bit more fidling found answer, returned needed.
select a.title, a.lat, a.lon, a.alert_content_id, a.date_added, count(r.alert_id) countrep ,count(i.alert_id) countint `alerts` left join `reply` r on r.alert_id = a.alerts left join `interactions` on i.alert_id = a.alerts group a.title, a.lat, a.lon, a.alert_content_id, a.date_added
Comments
Post a Comment