c_pattle Posted July 30, 2011 Share Posted July 30, 2011 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 More sharing options...
AyKay47 Posted July 30, 2011 Share Posted July 30, 2011 look into using a JOIN Link to comment https://forums.phpfreaks.com/topic/243316-mysql-query-help/#findComment-1249551 Share on other sites More sharing options...
fenway Posted July 31, 2011 Share Posted July 31, 2011 SELECT s.* , COUNT(h.status) FROM staff AS s LEFT JOIN hours AS h ON ( h.username=s.username AND h.status="sick" ) WHERE s.username="jbloggs" Link to comment https://forums.phpfreaks.com/topic/243316-mysql-query-help/#findComment-1249782 Share on other sites More sharing options...
c_pattle Posted July 31, 2011 Author Share Posted July 31, 2011 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 More sharing options...
Muddy_Funster Posted August 1, 2011 Share Posted August 1, 2011 SELECT s.* , COUNT(h.status) FROM staff AS s LEFT JOIN hours AS h ON ( h.username=s.username AND (h.status="sick" OR h.status = "Late") ) WHERE s.username="jbloggs" GROUP BY h.status Should work....I think Link to comment https://forums.phpfreaks.com/topic/243316-mysql-query-help/#findComment-1250198 Share on other sites More sharing options...
c_pattle Posted August 1, 2011 Author Share Posted August 1, 2011 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 https://forums.phpfreaks.com/topic/243316-mysql-query-help/#findComment-1250311 Share on other sites More sharing options...
Muddy_Funster Posted August 2, 2011 Share Posted August 2, 2011 did you try SELECT s.* , COUNT(h.status) FROM staff AS s LEFT JOIN hours AS h ON ( h.username=s.username AND (h.status="sick" OR h.status = "Late") ) WHERE s.username="jbloggs" GROUP BY h.status I messed up the tags on the last post. Link to comment https://forums.phpfreaks.com/topic/243316-mysql-query-help/#findComment-1250600 Share on other sites More sharing options...
kickstart Posted August 2, 2011 Share Posted August 2, 2011 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 More sharing options...
fenway Posted August 2, 2011 Share Posted August 2, 2011 Oops, for the GROUP BY -- thanks guys. Link to comment https://forums.phpfreaks.com/topic/243316-mysql-query-help/#findComment-1250663 Share on other sites More sharing options...
c_pattle Posted August 3, 2011 Author Share Posted August 3, 2011 Sorry Muddy_Funster I missed the group by on your first post. Thanks for the solution. And also thanks Keith! Link to comment https://forums.phpfreaks.com/topic/243316-mysql-query-help/#findComment-1251395 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.