Posted on

Let’s say you have a table that is filled with a list of transactions. And you want to run a query that will tell you the total value of those transactions. This one little line is all you need:SELECT SUM(table_name.column_name) FROM table_name

Perhaps you only want to know the total value of the transactions of just one customer? So long as you have a column designated to uniquely identify each customer, you can use something like this:

 SELECT SUM(table_name.column_name) FROM table_name WHERE user_id_column=1

If you are working within WordPress and want that user ID to be automatically generated based on the information of the logged in user who is using the page (perhaps this is a customer status page or a section you are building for their profile), you can call up the userid variable using this bit of PHP code:

$user_ID = get_current_user_id();

3 Replies to “How to Add Up Data in a Table Using SQL”

  1. You can use worksheet functions to create totals that are automatically updated as you add more data to a range or table, or to add only the unique values that are found in a range or table.

  2. Glad you have a donate button! I’m without a doubt donating to this brilliant blog!

    I look forward to fresh updates and will share this website with my Facebook group.

    Talk soon!

Leave a Reply

Your email address will not be published. Required fields are marked *