jwk811 Posted September 20, 2010 Share Posted September 20, 2010 $sql = "SELECT sum(tbl1.number) FROM tbl1, tbl2"; it only works if i have one table when i add the second it doesnt work. i just need the sum in one column from one table i just need other info from the other table too it wont work tho. im getting an entirely different number for the sum. Link to comment https://forums.phpfreaks.com/topic/213854-using-sum-but-having-more-than-one-table-in-select/ Share on other sites More sharing options...
btherl Posted September 20, 2010 Share Posted September 20, 2010 Can you be more specific about what you want? There's many ways you can combine data from two tables. Link to comment https://forums.phpfreaks.com/topic/213854-using-sum-but-having-more-than-one-table-in-select/#findComment-1113052 Share on other sites More sharing options...
jwk811 Posted September 20, 2010 Author Share Posted September 20, 2010 i want to count one column but get other info from more than one table at the same time Link to comment https://forums.phpfreaks.com/topic/213854-using-sum-but-having-more-than-one-table-in-select/#findComment-1113064 Share on other sites More sharing options...
mikosiko Posted September 20, 2010 Share Posted September 20, 2010 i want to count one column but get other info from more than one table at the same time here is a quick example for you to adjust to your tables... notice the way the tables are related using an INNER JOIN (in this case), and the GROUP BY clause allow you to give correct result for the aggregate function SUM SELECT P.productCode, P.productName, SUM(priceEach * quantityOrdered) total FROM orderdetails O INNER JOIN products P ON O.productCode = P.productCode GROUP by productCode ORDER BY total Link to comment https://forums.phpfreaks.com/topic/213854-using-sum-but-having-more-than-one-table-in-select/#findComment-1113069 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.