Skip to content
Advertisement

Count users with a condition in MYSQL

I have a table ‘helpdesk’ (detail below) that lists tickets.

I’m trying to get the top 10 users who report issues, however there is a complication in the fact that users can report an issue for another user (think secretary for their boss as an example).

The table structure is…

Using the example above the count should return

  • User 1 = 2 tickets (rows 1 & 2)
  • User 2 = 1 ticket (row 3)
  • User 3 = 0 (because they reported it for user 4)
  • User 4 = 1 ticket (row 4 as reported by user 3)

I can’t seem to get my head around it.

I’ve tried using an IF in the count(If(hd_forMe = 1, 1, 0)) and a CASE statement but this doesn’t then count the record for the user referenced in the hd_reportedFor column. I’ve also tried various combinations in the WHERE but can’t quite get that either.

These are some of the queries I’ve tried. The people table lists their names etc. and isn’t really important to the scope of this question.

And

Advertisement

Answer

Assuming that people_id is the primary key of people you should do a LEFT join of people to helpdesk (and not the other way around) and use a CASE expression in the ON clause, so that you pick the correct user:

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement