Jump to content

MySQL query help


c_pattle

Recommended Posts

I have a table called "staff" which has a list of employees details.  I also have a table called hours which stores employee hours. 

 

What I want to do is the have a query that selects all the fields from the staff table such as

 

SELECT * FROM staff WHERE username="jbloggs"

 

and also to select the number of times this employee has called in sick such as

 

SELECT COUNT(status) FROM hours WHERE status="sick" and username="jbloggs"

 

Is there an easy way to merge these two sql queries into one?

 

Thanks for any help

Link to comment
Share on other sites

Thanks for your help.  I understand it now.  Sorry another question.  Is it also possible to perform a count on the same field but using a different condition?  What I also want to do is to count the number of times h.status="late". 

 

Would I be able to do this in the same query because I'm already performing a count on this field?

Link to comment
Share on other sites

Thanks.  Yeah that should work but what I want to do is to perform a different count for each criteria.  So the final result set will have seperate count for when the status is "sick" and a seperate one where the status is "late".  Is this possible?

 

Thanks

Link to comment
Share on other sites

Hi

 

Or, if you want each person to be on a line (so not a separate row for sick and late):-

 

SELECT s.*, COUNT(h.status), COUNT(l.status)
FROM staff AS s
LEFT JOIN hours AS h ON ( h.username=s.username AND h.status="sick")
LEFT JOIN hours AS l ON ( l.username=s.username AND  h.status = "Late" )
WHERE s.username="jbloggs"
GROUP BY s.username

 

All the best

 

Keith

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.