Jump to content

Archived

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

tobeyt23

[SOLVED] Subquery

Recommended Posts

When i do the querys together the sum for total returns nothing however when i do them seperately they work fine, what am i doing wrong here:
[code]
SELECT zc.* FROM zip_code zc LEFT JOIN zip_holder zh ON zc.zip_code_id=zh.zip_code_id,
(SELECT SUM( zc1.quantity ) FROM zip_code zc1 LEFT JOIN zip_holder zh1 ON zc1.zip_code_id = zh1.zip_code_id WHERE zh1.cart_id='41a0efa279f4a129bb4064835b5a1fb9' GROUP BY zh1.cart_id) AS Total WHERE zh.cart_id='41a0efa279f4a129bb4064835b5a1fb9'[/code]

Share this post


Link to post
Share on other sites
That is strange; but why don't you have this as a single query? Try the following (UNTESTED):

[code]SELECT zc.*, SUM( zc.quantity ) AS Total FROM zip_code zc LEFT JOIN zip_holder zh ON zc.zip_code_id = zh.zip_code_id WHERE zh.cart_id='41a0efa279f4a129bb4064835b5a1fb9'[/code]

Hope that helps.

Share this post


Link to post
Share on other sites
I tried this but when you did it as one you need to GROUP BY which doesn't return what i need

Share this post


Link to post
Share on other sites
I don't understand why -- you want the SUM() all of the matching rows with the given cartID. The GROUP BY is implicit. Please clarify.

Share this post


Link to post
Share on other sites

×

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.