How to Add Up Data in a Table Using SQL

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();

Leave a Reply

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