guyfromfl Posted September 22, 2009 Share Posted September 22, 2009 ok lets say I have a table -------------------------------- | id | invoiceId | qty | price | -------------------------------- |1 | 10354 | 2 | 5 | |2 | 10356 | 3 | 6 | |3 | 10354 | 4 | 1 | -------------------------------- I want to multiply qty * price where invoiceId=10354 then add the sum of each. would it be better to just add a total column? or is there a sql function that i could use? thanks Quote Link to comment https://forums.phpfreaks.com/topic/175070-solved-multiply-2-columns-and-add-them/ Share on other sites More sharing options...
smerny Posted September 22, 2009 Share Posted September 22, 2009 what do you want to do with the information? just output it? use the query SELECT qty, price WHERE invoiceID='10354' and store it to $row or something.... then $total = $row['qty'] * $row['price']; Quote Link to comment https://forums.phpfreaks.com/topic/175070-solved-multiply-2-columns-and-add-them/#findComment-922678 Share on other sites More sharing options...
guyfromfl Posted September 22, 2009 Author Share Posted September 22, 2009 Yea im making an invoice system in php. I have 2 tables parts.invoice (customer data record of the invoice....this has a total field) and parts.invoiceitems (individual things a customer buys.) so if I add a new item after the invoice is generated, I want to have a new total to put in parts.invoice.total. and when I add an item to the order, I have to update the invoice total. I have php creating the subtotals for the invoice, but I was tinkering with the idea of doing it all on the database. Any suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/175070-solved-multiply-2-columns-and-add-them/#findComment-922693 Share on other sites More sharing options...
Philip Posted September 22, 2009 Share Posted September 22, 2009 I'm not sure what you want to add... all of the multiplication products? here's for multiplying: SELECT (qty * price) as total FROM table WHERE id = 10354 You can use SUM to get the total.... but I'd do that in PHP Quote Link to comment https://forums.phpfreaks.com/topic/175070-solved-multiply-2-columns-and-add-them/#findComment-922713 Share on other sites More sharing options...
guyfromfl Posted September 22, 2009 Author Share Posted September 22, 2009 basically multiply qty and price from row 1, then mulitply qty and price from row 3 then add it together. My script is already doing it in php, I just wanted to see how it works if I get it from the db and leave it all in the mysql database. Quote Link to comment https://forums.phpfreaks.com/topic/175070-solved-multiply-2-columns-and-add-them/#findComment-922734 Share on other sites More sharing options...
Philip Posted September 22, 2009 Share Posted September 22, 2009 If you just want the sum, and not data from each row: SELECT SUM(qty * price) as total FROM table WHERE id = 10354 Quote Link to comment https://forums.phpfreaks.com/topic/175070-solved-multiply-2-columns-and-add-them/#findComment-922948 Share on other sites More sharing options...
guyfromfl Posted September 23, 2009 Author Share Posted September 23, 2009 Yea thats it, I dont know why I was getting an error when i was trying that before, i think its because I didnt tell it what to alias it as maybe? I dunno but thats what I was looking for Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/175070-solved-multiply-2-columns-and-add-them/#findComment-923262 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.