Range data - mysql query

Discussion in 'MySQL' started by amitshah.in, Sep 6, 2009.

  1. #1
    I have two tables in my database.
    
    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 
    
    Code (markup):
    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
     
    amitshah.in, Sep 6, 2009 IP
  2. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You should add a category_id foreign key to the products table, then just do a left join and group it.
     
    premiumscripts, Sep 7, 2009 IP
  3. Lavee

    Lavee Well-Known Member

    Messages:
    234
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    108
    #3
    Try this,

    Thanks

    Lavee
     
    Lavee, Sep 9, 2009 IP
  4. amitshah.in

    amitshah.in Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Sorry Lavee, it is not working..
     
    amitshah.in, Sep 9, 2009 IP
  5. amitshah.in

    amitshah.in Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Ok, I resolved the issue. Find below query for same.

    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
    Code (markup):
    Regards,
    Amit
     
    amitshah.in, Sep 9, 2009 IP