Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.
Hi all, please see this major site announcement: https://www.boards.ie/discussion/2058427594/boards-ie-2026

Adding values in a mysql database

  • 01-03-2006 05:52PM
    #1
    Closed Accounts Posts: 8,866 ✭✭✭


    I have a table, tbl_hours, which saves hours spent on a specific task into the field hours_spent, and also has a unique id hours_task_id. I want to create a page that will sum up the total hours for a specific task based on the hours_task_id.

    So I will have a page listing existing tasks, from tbl_tasks, and when i click on a task it shows me a page that will give me details on the task, which i can select from tbl_task, and the total hours summed from the hours_spent field where the task_id = hours_task_id.

    Am I making sense? :D


Comments

  • Registered Users, Registered Users 2 Posts: 68,173 ✭✭✭✭seamus


    SELECT SUM(hours_spent) from tbl_hours WHERE task_id = hours_task_id GROUP BY hours_task_id

    Should work.


  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    And how would I print that in the table? I.e. the task name is printed out with:

    print("<td> ".$row."</td>");

    but how do I print the results of the sum() function?


  • Registered Users, Registered Users 2 Posts: 68,173 ✭✭✭✭seamus


    Yeah, forgot that. It can be a bit finnicky. Easier to write

    SELECT SUM(hours_spent) as total_hours_spent from tbl_hours WHERE task_id = hours_task_id GROUP BY hours_task_id

    And then just use $row to output it.


  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    Works a charm, cheers!


Advertisement