Jump to content

Recommended Posts

Hello Gurus,

 

I've tried all day to get this query right, but no luck so I finally had to break down and ask for help.  I'm using MySQL 5.0

 

I have a reservation system where products can be reserved.  The reservation_items table is like so:

id  |  specialProductID  |  productID  | quantity

1              101                    0              2

2                0                    102            3

 

The specialProductID is actually a bundle of products with their own quantities tied to a special_items table like so:

  specialProductID  |  productID  | quantity

      101                        103            2

      101                        104            1

 

My goal is to display the top 3  reserved products based on highest quantity individually.  So in the case above my repeat region would show this order:

Top products

103 quantity of 4

102 quantity of 3

104 quantity of 2

 

When the reservation item is not part of a bundle then the quantity in the reservation_items table is the quantity, if it's a special product (a bundle of products) the reservation_items quantity is multiplied by the special product quantity.  I have gotten my query close, but not quite what I need.

 

SELECT 
  SUM(productQuantity * specialQuantity) AS Specials_Quantity,
  SUM(productQuantity) AS Products_Quantity
FROM
  reservations
  INNER JOIN reservation_items ON (reservations.reservationID = reservation_items.reservationID)
  INNER JOIN special_items ON (reservation_items.specialProductID = special_items.specialProductID)
  INNER JOIN products ON (special_items.productID = products.productID)
GROUP BY
  productQuantity
  ORDER BY Specials_Quantity, Bottles_Quantity

 

I believe I also need to add a WHERE reservation_items.specialProductID<>0 to the Special_Quantity SUM and WHERE reservation_items.specialProductID=0 to the Bottles_Quantity but I don't know the proper syntax.

 

Any help would be greatly appreciated.

 

Thanks in advance,

Twitch

 

 

Link to comment
https://forums.phpfreaks.com/topic/231735-where-and-sum-issue/
Share on other sites

Hi Fenway, thanks for the reply.  I wasn't sure what HAVING was, but since posting this the difference between WHERE and HAVING was explained to me like so:

 

"It's like asking someone to fetch blue balls from a dark room filled with different colored balls and cubes. You can tell them to SELECT balls FROM room WHERE shape='round' HAVING color='blue'. While performing the query (fetching the items), the shape is used to limit the number of retrieved items, but the color is not known until after the query has finished and the balls are brought out into the light, and as such they have to be discarded after everything is retrieved."

 

I haven't had a chance to go back and reexamine this query.  I'm just now getting into more complicated queries and using aliases etc.

 

Once again, thanks a bunch for the reply.  Hopefully, I'll get to re-write this query tonight.

 

Sincerely,

Twitch

Link to comment
https://forums.phpfreaks.com/topic/231735-where-and-sum-issue/#findComment-1195731
Share on other sites

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.