build a query

Discussion in 'MySQL' started by ahdsan, Feb 17, 2014.

  1. #1
    I have a table with columns, itemid, itemname, price1, price2, price3, price4, price5, price6, price7, etc. These are different prices from different distributors.
    I am displaying these records in a table thru php.
    What I want is , get 3 lowest distributor prices.
    Forexample,
    item1 ->
    lowest from price1, price2,price3, price4.....
    second lowest from price1, price2, price3, price4....
    third lowest from price1, price2, price3, price4....

    Can anyone give an idea ?
     
    ahdsan, Feb 17, 2014 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    The constructions of the tables are "wrong" - what you should have is two tables, one for itemid, itemname and a second for prices with id, itemid and price and then have several entries in this for each item.
    That way you will get something like
    SELECT t1.*,t2.* FROM items t1 LEFT JOIN itemprices t2 ON t1.itemid = t2.itemid ORDER BY price
     
    PoPSiCLe, Feb 17, 2014 IP
  3. ahdsan

    ahdsan Member

    Messages:
    74
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #3
    i cannot change the table structure now as it is coming from a scraper.
    I have one item and multiple price columns.
    with this structure is it possible to get lowest prices across columns for each row ??
     
    ahdsan, Feb 17, 2014 IP
  4. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    #4
    koko5, Feb 17, 2014 IP
  5. ahdsan

    ahdsan Member

    Messages:
    74
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #5
    thanks. I think this might work, I will try it out. I have never used this function so will try it out.
     
    ahdsan, Feb 17, 2014 IP