How to Add & Subtract Integers in Different Columns in MySQLby Jim Campbell
The MySQL database engine includes functions that allow you to add and subtract different columns in a database table. MySQL uses the plus sign and negative sign to add and subtract integers in a list of columns. You use this feature to add values in a report or return the calculation to a Web page or desktop application.
Open the MySQL Query Browser software from the Windows "Start" menu in the "MySQL" program group.
Log in to your MySQL database with your username and password. After the software opens, click the "New Query" button to open the SQL editor.
Type the SQL code to add a list of columns. For instance, the following code adds two order columns in a customer "orders" table:
select sum(order1+order2) from orders where customer_id=111
In this example, the sum of all orders for customer_id 111 is calculated. Create your own where clause to sum the columns for a specific type of data.
Add the MySQL statement to subtract columns. The following code shows you how to subtract columns:
select sum(order1) - sum(returned_items_total) from orders where customer_id=111
In this example, the sum of all returned items is subtracted from the sum of all the orders to get profit from the table. Again, the where clause filters the rows, so only customer_id 111 is calculated.
Click the "Execute" button to run the two MySQL statements. The results of the calculations show up at the bottom of the Query Browser window.
Click here to provide feedback on this article