WebProWorld Part of WebProNews.com
Page One Link To Us Edit Profile Private Messages Archives FAQ RSS Feeds  
 

Go Back   WebProWorld > Webmaster, IT and Security Discussion > Database Discussion Forum
Subscribe to the Newsletter FREE!


Register FAQ Members List Calendar Arcade Chatbox Mark Forums Read

Database Discussion Forum This is the place to find help resolving those nagging questions you have about implementing and using all kinds of databases. Need help writing a query? Need an opinion on Oracle? Post here!

Closed Thread
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-22-2007, 09:25 AM
wige's Avatar
wige wige is offline
Moderator
WebProWorld Moderator
 

Join Date: Jun 2006
Location: United States
Posts: 1,648
wige RepRank 4wige RepRank 4wige RepRank 4
Default Need help creating a MySQL query

I am looking for any help or suggestions I can get to develop an SQL query, and everywhere I have looked I have been unable to find any examples or tutorials that cover this.

I have a MySQL database powering my web site. The database that contains contains the following two tables, one with a list of products (products: SKU, name, description, manufacturer) and the other with a list of orders (orders: orderID, customerID, SKUordered).

The information I need to get from the database is a list of product names from a certain manufacturer (eg SELECT name FROM products WHERE manufacturer='somecompany') and how many times those specific SKUs were ordered (For each result, how many orders table records have SKUordered=SKU of that match) The result I am hoping to get would be in the following form: productname, numberOfOrders for each product by the manufacturer.

Any suggestions you can give me would be greatly appreciated. If you need any clarification on anything, please let me know.
__________________
The best way to learn anything, is to question everything.
Interestingly Average Security Blog
  #2 (permalink)  
Old 02-22-2007, 12:47 PM
brian.mark's Avatar
brian.mark brian.mark is offline
Administrator
 

Join Date: Jul 2004
Location: Omaha
Posts: 2,717
brian.mark RepRank 2brian.mark RepRank 2
Default

Code:
SELECT products.name, COUNT(orders.orderID) AS numberOfOrders FROM products LEFT JOIN orders ON products.sku = orders.SKUordered WHERE products.manufacturer = 'somecompany' GROUP BY orders.SKUordered;
That should do what you'd like. You could also add an order by clause if you want to get the most ordered items first.

That'll be number of orders an item appears on. Assuming you have a quantity field in the orders table, you could also do sum(quantity) in place of the count to get the most sold items.

Be aware that this will be very CPU intensive if you've got a large number of records. It took well over 1 minute on our machines to do a similar query, but hopefully you won't have as much data to try to join together.

Brian.
__________________
ToolBarn.com, an Internet Retailer Top 500 and Inc. 500 Company | Tool Parts | Pet Supplies
  #3 (permalink)  
Old 02-22-2007, 03:03 PM
wige's Avatar
wige wige is offline
Moderator
WebProWorld Moderator
 

Join Date: Jun 2006
Location: United States
Posts: 1,648
wige RepRank 4wige RepRank 4wige RepRank 4
Default

Great, seems to work, except that it only returns records for items that have been ordered. Those products that have no associated orders do not show in the results. Is there a change that would get these other records to show up?

-- Edit --
Actually, it works perfectly, I just had a typo in the Group By. Thanks for the help!
__________________
The best way to learn anything, is to question everything.
Interestingly Average Security Blog
  #4 (permalink)  
Old 02-22-2007, 03:44 PM
brian.mark's Avatar
brian.mark brian.mark is offline
Administrator
 

Join Date: Jul 2004
Location: Omaha
Posts: 2,717
brian.mark RepRank 2brian.mark RepRank 2
Default

Quote:
Originally Posted by wige
Great, seems to work, except that it only returns records for items that have been ordered. Those products that have no associated orders do not show in the results. Is there a change that would get these other records to show up?

-- Edit --
Actually, it works perfectly, I just had a typo in the Group By. Thanks for the help!
Glad it's working for you. Enjoy.

Brian.
__________________
ToolBarn.com, an Internet Retailer Top 500 and Inc. 500 Company | Tool Parts | Pet Supplies
  #5 (permalink)  
Old 05-18-2007, 12:12 AM
ajel.smith ajel.smith is offline
WebProWorld New Member
 

Join Date: May 2007
Posts: 1
ajel.smith RepRank 0
Default

Hi all
I am a new member of this group.Tell me In MYSQL ,why we firstly enters @ sign in any query.
Thnx.Free Music
  #6 (permalink)  
Old 05-18-2007, 02:57 PM
Vectorman211's Avatar
Vectorman211 Vectorman211 is offline
Moderator
WebProWorld Moderator
 

Join Date: Jul 2006
Posts: 89
Vectorman211 RepRank 2
Default

Quote:
Hi all
I am a new member of this group.Tell me In MYSQL ,why we firstly enters @ sign in any query.
Thnx.Free Music
Umm... what?
__________________
http://www.wis-tech.net
Closed Thread

  WebProWorld > Webmaster, IT and Security Discussion > Database Discussion Forum
Tags: creating, help, mysql, query



Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



Search Engine Friendly URLs by vBSEO 3.0.0