Jump to content

using SUM() but having more than one table in SELECT


jwk811

Recommended Posts

$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.

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.