Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can use <code>SUM(condition)</code> on a single use of the <code>tbl_user_work_settings</code> table:</p> <pre><code>// get total days worked, unpaid days, bank holidays, holidays, sicknesses // and absences within given date range for given users $sql = " SELECT users.username, SUM(ws.isUnpaid ='1') AS unpaidDays, SUM(ws.isBankHoliday ='1') AS bankHolidays, SUM(ws.isHoliday ='1') AS holidays, SUM(ws.isSickness ='1') AS sicknesses, SUM(ws.isOtherAbsence='1') AS absences, COUNT(DISTINCT DATE(cl.in_time)) AS daysWorked, SUM(cl.seconds_duration) AS secondsWorked FROM users LEFT JOIN tbl_user_work_settings AS ws ON ws.username = users.username AND ws.selectedDate BETWEEN '$startDate' AND '$endDate' LEFT JOIN tbl_clockins AS cl ON cl.username = users.username AND DATE(cl.in_time) BETWEEN '$startDate' AND '$endDate' WHERE users.clockin_valid='1'"; if(count($selectedUsers)&gt;0) $sql .= " AND users.username IN ('" . implode("','", $selectedUsers) . "')"; $sql .= " GROUP BY users.username ORDER BY users.username ASC"; </code></pre> <p>By the way (and perhaps more for the benefit of other readers), I <strong>really</strong> hope that you are avoiding SQL injection attacks by properly escaping your PHP variables before inserting them into your SQL. Ideally, you shouldn't do that at all, but instead pass such variables to MySQL as the parameters of a prepared statement (which don't get evaluated for SQL): read more about <a href="http://bobby-tables.com" rel="nofollow">Bobby Tables</a>.</p> <p>Also, as an aside, why are you handling integer types as strings (by enclosing them in single quote characters)? That's needless and a waste of resource in MySQL having to perform unnecessary type conversion. Indeed, if the various <code>isUnpaid</code> etc. columns are all <code>0</code>/<code>1</code>, you can change the above to remove the equality test and just use <code>SUM(ws.isUnpaid)</code> etc. directly.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload