Eventhosts – containing the three regular hosts and an “other” field (if someone is replacing them) or edit: being a guest host (in addition to the regulars)
eventid | host (SET[Steve,Tim,Brian,other])
-------------------------------------------
1 | Steve
2 | Tim
3 | Brian
4 | Brian
5 | other
6 | other
Event
id | other | name etc.
----------------------
1 | | …
2 | | …
3 | | …
4 | Billy | …
5 | Billy | …
6 | Irwin | …
This query:
SELECT h.host, COUNT(*) AS hostcount
FROM host AS h
LEFT OUTER JOIN event AS e ON h.eventid = e.id
GROUP BY h.host
Returns:
Steve | 1
Tim | 1
Brian | 2
other | 2
I want it to return:
Steve | 1
Tim | 1
Brian | 2
Billy | 1
Irwin | 1
OR:
Steve | | 1
Tim | | 1
Brian | | 2
other | Billy | 1
other | Irwin | 1
And not:
Steve | | 1
Tim | | 1
Brian | | 1
Brian | Billy | 1
other | Billy | 1
other | Irwin | 1
Can someone tell me how I can achieve this or point me in a direction?
Use this:
Just a note, don’t use
COUNT(*), if a host don’t have event, it will show 1 instead of 0. UseCOUNT(e.*)For the last result, use this:
[EDIT]
Tried the following query(i.e. without the suggested repetition of fields on GROUP BY), it also works on your original question and your edited question. I just installed MySQL now, I don’t know if it has bearing on database type, I only enabled InnoDB and strict settings. By the way
COUNT(e.*)(which is ANSI SQL-accepted I presume) doesn’t work on MySQL, instead must useCOUNT(e.id)(or maybe you already amended in your query):