user2189151
user2189151

Reputation: 47

creating a specific query using mysql and php

I need help creating a specific query, The following is an example of my deposit table, empId is a foreign key that refers to the primary key of my 'users' table which is 'userId' Note:users table is not shown here

  mysql> SELECT * FROM deposit
    -> ;
+------------+---------------+---------+-------------+-------------+-------------+-------+
|    CheckId | jobId         | payRate | jobLocation | hours       | date_paid   | empId |
+------------+---------------+---------+-------------+-------------+-------------+-------+
|       1512 | entertainment |      12 | store1      |       10.00 | 2013-03-02  |     1 |
|       1510 | entertainment |      12 | store1      |        8.00 | 2013-03-01  |     1 |
|       1507 | retail        |      10 | store1      |        8.00 | 2013-03-18  |     1 |
|       1506 | retail        |      10 | store1      |       20.00 | 2013-03-19  |     1 |
+------------+---------------+---------+-------------+-------------+-------------+-------+

What i want is to calculate the sum of all hours for all specific jobId , in this case if i did the query correctly it would look like this:

+---------------+---------------+---------+
| payID         | payRate       | hours   | 
+---------------+---------------+---------+
|  entertainment| 12            |      18 | 
|        retail | 10            |      28 | 
+---------------+---------------+---------+

In this case there is only two jobIds but it could have more than 2

This is the query i have and its only showing one payId, so I need help fixing it

also note that email is an attribute of my users table

  <table>";
             $query = "SELECT jobId, payRate, SUM(hours) AS 'All_Hours'
                       FROM users INNER JOIN deposit ON userId = empId
                      WHERE users.email = '" . $_SESSION['email'] ."' 
                      GROUP BY jobId,payRate";

                      if (!$result) { //if the query failed
                          echo("Error, the query could not be executed: " .
                          mysqli_error($db) . "</p>");
                          mysqli_close($db); //close the database
                      } //by now we have made a successful query  
                      while ($row = mysqli_fetch_assoc($result)){
                      echo "<tr><td>" .$row['jobId'] . "</td>
                            <td>" .$row['payRate'] . "</td>
                            <td>" .$row['All_Hours'] . "</td>
                            </tr>";
                      }
              echo"</table>

Upvotes: 1

Views: 74

Answers (2)

Andrew
Andrew

Reputation: 7778

On the deposit table

SELECT jobId, payRate, sum(hours) FROM deposit
group by 1, 2

I would also suggest that you make it little bit more relational: jobId and jobLocation needs to be moved to new tables

Upvotes: 0

John Woo
John Woo

Reputation: 263723

you forgot to add GROUP BY clause in your query causing to have only one record in the result,

SELECT jobId, payRate, SUM(hoursWorked) AS 'All_Hours'
FROM   users INNER JOIN paycheck ON userId = empId
WHERE  users.email = 'session_email_here' 
GROUP  BY jobId, payRate

Upvotes: 3

Related Questions