I have 3 tables which are part of my database.
debates (id 'PK', unit_id, starter_pack_id 'FK', title)
debate_stakeholders (id 'PK', starter_pack_id 'FK', name)
debate_groups (id 'PK', debate_id 'FK', student_id, stakeholder_id 'FK')
For this purpose all debates share the same stakeholders (4 stakeholders in total, all of these stakeholders are referenced for all debates).
The aim of my expected outcome to query all the debates, that shows the debates.id, debates.title, debate_stakeholders.name, and the Count of how many of those stakeholders occur within that particular debate, in relation to the relative stakeholder irrespective if the count of stakeholders is 0. This part is important as when I perform additional queries, I need to know which query counts are greater than or equal to one, zero and null.
Here is the sample data of my database:
My expected outcome: (The count is just to show what It could look like)
I have attempted to create this MySQL query, but I am unable to achieve my exact requirements.
I have tried queries such as
SELECT
a.id,
a.name,
a.abbreviation,
d.id AS debateId,
IF(COUNT(b.stakeholder_id) = 0, 0, COUNT(b.stakeholder_id)) AS total_freq
FROM
debate_stakeholders a LEFT JOIN debate_groups b ON b.stakeholder_id = a.id
LEFT JOIN debates as d ON b.debate_id = d.id
GROUP BY
a.id, b.debate_id,d.id
HAVING
COUNT(*) < 3
ORDER BY a.id,d.id
LIMIT 1
But that hasen't quite planned out.


idand alsostarter_pack_id? Why is this?LEFT JOIN? Have a look here.