/    Sign up×
Community /Pin to ProfileBookmark

Need help formatting a SELECT query (w/subqueries?)

I have a database for a shopping cart program, and I want to create a page that my boss can access to see which products have been purchased. The query should produce output like this:

Product Code | Product Name | Quantity Sold | Total Value
—————————————————–

LAP-123 | White Envelopes | 203 | $265.02

Orders are stored in two tables: Orders, which creates an ID per order and stores details like the total order amount, and Order_Details which links the productid’s ordered and the number of each product ordered with the orderid from Orders.

There’s also a Products table, which has productid’s, product name and product codes.

There’s also a Pricing table, which is where the prices for each product are stored.

I have the basic query down already, I can make it print all the products that have been ordered. What I need it to do is combine all the occurances of each product together and then total the quantity sold and the combined value sold.

Here’s what I have:

[code=php]SELECT o.productid,o.product_options, o.amount,
prod.productid, prod.productcode,
prod.product FROM xcart_order_details AS
o, xcart_products AS prod WHERE
o.productid = prod.productid[/code]

What that does is print out the product code, the product name/product options, and the number sold, but there are repeating instances of products that have been ordered more than once. I suspect I need the GROUP BY modifier, but I don’t understand how to do that. I also need to get the Pricing table involved somehow, I think.

Can anybody help?

to post a comment
PHP

0Be the first to comment 😎

×

Success!

Help @gianna spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...