Jump to content

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


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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.