Jump to content

calculations using mysql


Slowie

Recommended Posts

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

Link to comment
Share on other sites

ok so going on that my current code is

 

 

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']."'

 

so the code after the changes will be

 

SELECT options.company, options.user_level, options.branch, Count(stafflist.userlevel) AS userlevel 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 
LEFT JOIN stafflist on stafflist.branch=options.branch
WHERE options.user_level != '0' 
AND options.user_level != '2' 
AND options.user_level != '4' 
AND options.company='".$_SESSION['company']."'
GROUP BY options.branch, stafflist.userlevel

 

would this be correct?

Link to comment
Share on other sites

currently with

 

$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']."'
  " ; 

 

i get the branch along with the userlevel

 

so that is

 

1 - 1

1 - 2

1 - 3

2 - 1

2 - 2

2 - 3

 

now i know i need to left join in the stafflist table and do a count similar to this one

 

SELECT branch, user_level, COUNT(*) AS staff
FROM stafflist
GROUP BY branch, user_level

 

this gives me the results i want but i need this in one mysql query for a table i just cant work out how to do it :(

 

Link to comment
Share on other sites

i solved it in the end the query i was looking for was as below. but thank you for the help everyone

 

SELECT options.optionsid, userlevel.Description, branch.branch, COUNT(*) AS Staff, stafflist.user_level
FROM stafflist
Left Join userlevel on stafflist.user_level=userlevel.level
Left Join branch On stafflist.branch=branch.branchid
Left Join options On userlevel.level=options.user_level Where options.branch=stafflist.branch
GROUP BY branch, user_level

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.