slowie
slowie

Reputation: 51

calculation with mysql im having a trouble working it out

hey guys ive been trying to do this one all night and i dont know if im either approching it wrong or its just not possible. i have tried multiple ways of doing it but havent succeded.

basically i have 2 tables one called user level and one called stafflist

now in the user level table i have 2 companies with 3 userlevels for each so

company userlevel
1   1
1   2
1   3
2   1
2   2
2   3

now in the stafflist table there are lets say 40 staff members and each have a field filled in which is the company with either 1 or 2 they also each have a user level assigned. so 1 , 2 or 3

what i want is for the query to count how many staff are assigned each level for that company so the end result is a table like

company userlevel number of staff
1   1         15
1   2         3
1   3         2
2   1         18
2   2         2
2   3         0

could someone explain how i would go about this. ill figure out the exact code but i just need a shove in the right direction an exaple would be really appreciated

my current code is

$sql = "select options.company, options.user_level, options.branch, userlevel.Description,  branch.branch as branch from options 
      left join branch on options.branch=branch.branchid 
      left join userlevel on options.user_level=userlevel.level 
      where options.user_level != '0' And options.user_level != '2' And options.user_level != '4' AND options.company='".$_SESSION['company']."'
      " ;  

Upvotes: 0

Views: 66

Answers (1)

user1191247
user1191247

Reputation: 12973

The basics of the query would be -

SELECT company, userlevel, COUNT(*)
FROM stafflist
GROUP BY company, userlevel

Upvotes: 1

Related Questions