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
https://forums.phpfreaks.com/topic/243316-mysql-query-help/
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
https://forums.phpfreaks.com/topic/243316-mysql-query-help/#findComment-1249814
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
https://forums.phpfreaks.com/topic/243316-mysql-query-help/#findComment-1250627
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.