If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Query to select range

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-06-09, 03:33
amitshah.in amitshah.in is offline
Registered User
 
Join Date: Jan 2009
Location: Ahmedabad
Posts: 4
Query to select range

I have two tables in my database.
Code:
First Table Name: product_price_range
Field                          Type             
-----------------------------  
price_category_id              int(11)    
title                          varchar(155)  
min_price                      float          
max_price                      float       


Data: product_price_range
price_category_id  title        min_price  max_price 
-----------------  -----------  ---------  --------- 
                1  1-49                 1         49 
                2  50-99               50         99 
                3  100-199            100        199 
                4  200-299            200        299 
                5  300-erboven        300          0 

Second Table: product
Field               Type             
-----------------------------  
product_id           int(11)    
title                varchar(155)  
price                float          


Data: product
product_id  title            price  
---------- -----------  ---------
           1  product1       49 
           2  product2       99 
           3  product3       149 
           4  product4       249 
           5  product5       300
Now I want to show the only those price range in which product price is available. For that I need a query or other possible way to identify those price range.
In above example output should be
1. 1-49
2. 50-99
4. 200-299

Can anyone help me out in this issue?

Regards,
Amit Shah
Reply With Quote
  #2 (permalink)  
Old 09-06-09, 04:33
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Code:
SELECT DISTINCT ppr.title
  FROM product_price_range AS ppr
INNER
  JOIN product AS p
    ON p.price BETWEEN ppr.min 
                   AND CASE WHEN ppr.max = 0 
                            THEN 9999999999
                            ELSE ppr.max END
the join would've been cleaner if you hadn't used 0 as the max price

also, be careful using FLOATs -- you really should be using INTEGER for these prices

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 09-06-09, 06:24
amitshah.in amitshah.in is offline
Registered User
 
Join Date: Jan 2009
Location: Ahmedabad
Posts: 4
Thanks for query. It is working fine..
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

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