简体   繁体   中英

MySQL Sum with Join

I have been trying to do a sum with a join with no luck. What I need is to get a total from product_price (located in tbl_products) based on a product_id which is located in both tbl_basket and tbl_products.

So far I have:

$result = mysql_query("SELECT * FROM tbl_basket a INNER JOIN tbl_products b ON a.product_id = b.product_id WHERE a.customer_id = '" . $_SESSION['user'] . "'");

This works to get the product_name from tbl_products based on the product_id stored in tbl_basket. I know I need a COUNT which I have tried numerous ways but with no luck. I have chosen to show a query I know works for something as my syntax for the COUNT I tried was way off. Thank you.

这行不通吗?

$result = mysql_query("SELECT sum(b.product_price) as total_price FROM tbl_basket a INNER JOIN tbl_products b ON a.product_id = b.product_id WHERE a.customer_id = '" . $_SESSION['user'] . "'")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM