MySQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesMySQL Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 17th, 2012, 09:47 AM
jstanton jstanton is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 2 jstanton User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 22 m 19 sec
Reputation Power: 0
Help needed for nested SELECTs

Hi there

I have two tables, one containing products and one containing options for each product. The options table has an ID field to link the records to the products table.

When showing a list of products, I also want to show the cheapest price from the options table.

eg. Product 1 is paint. It has three options - white £9.99, black £10.99 and colour £14.99.

When listing the products I want it to say 'Paint - From £9.99'.

I've been trying to construct a query which has a nested SELECT statement to get the MIN value for price in the options table but I just can't get it to work.

Could someone help me with how I construct this?

Many thanks in anticipation,

Jon

Reply With Quote
  #2  
Old November 17th, 2012, 09:52 AM
MrFujin's Avatar
MrFujin MrFujin is offline
Lord of the Dance
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Oct 2003
Posts: 3,129 MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 22 h 45 m 15 sec
Reputation Power: 1736
Would be great if you had posted the SQL you tried.

General, you should be able to get the information with something like this: (untested)
Code:
SELECT	p.ProductName , MIN(po.Price)
FROM	Products p 
INNER JOIN ProductOptions po ON p.pid = po.pid
WHERE p.ProductName = "Product 1"
GROUP BY p.ProductName

Reply With Quote
  #3  
Old November 17th, 2012, 10:02 AM
jstanton jstanton is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 2 jstanton User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 22 m 19 sec
Reputation Power: 0
Now that's what I call a speedy response! Many thanks for that. I didn't paste my attempt because it's already a lot more complicated than just the issue I posted, but I was already going down the wrong path in trying a SELECT in the FROM part. Here's that bit:

...
FROM products, (SELECT MIN(product_options.poPrice) AS fromprice FROM product_options WHERE product_options.poProductID = products.ProductID) AS priceoptions
...

I think I got it completely wrong! I'll try your suggestion now.

Thanks again.

Jon


Quote:
Originally Posted by MrFujin
Would be great if you had posted the SQL you tried.

General, you should be able to get the information with something like this: (untested)
Code:
SELECT	p.ProductName , MIN(po.Price)
FROM	Products p 
INNER JOIN ProductOptions po ON p.pid = po.pid
WHERE p.ProductName = "Product 1"
GROUP BY p.ProductName

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMySQL Help > Help needed for nested SELECTs

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap