Results 1 to 6 of 6

 

Thread: Mysql problem - probably obvious to fix

  1. #1
    Registered User

    Status
    Offline
    Join Date
    Sep 2003
    Posts
    315
    Thanks
    28
    Thanked 19 Times in 16 Posts


    Hi,

    Got a problem that should be simple but I can't get my head around it.

    I have a table called offers

    And it looks like

    retailer 1|red chair|£29.99
    retailer 2|red chair|£39.99
    retailer 4|red chair|£59.99
    retailer 5|red chair|£69.99

    retailer 1|blue chair|£69.99
    retailer 2|blue chair|£59.99
    retailer 4|blue chair|£49.99
    retailer 5|blue chair|£59.99

    I want to write a query that says:

    Select all red chairs where the best price is over £50. I can write a query that brings back:

    retailer 4|red chair|£59.99

    Easily, but this isn't the best price for this item.

    Here's what I've tried, that doesn't do the job:

    SELECT product_name, price
    FROM offers
    WHERE category = 'chairs'
    AND price > 50
    GROUP BY product_name
    ORDER BY price ASC

    Thanks!

  2. #2
    scriptmonkey's Avatar
    Oranges & Lemons

    Status
    Offline
    Join Date
    Jan 2009
    Location
    Worthing
    Posts
    1,607
    Thanks
    112
    Thanked 253 Times in 192 Posts
    From the data you've listed then the query is returning exactly what you asked for, i.e. the lowest price over £50, which is £59.99

    I can't see where you'll get a better price (from the data you've listed), have you got any prices between £50 and £59.99?
    The trouble with the rat race is that even if you win you're still a rat.
    Time passes. Listen. Time passes. Dylan Thomas
    Ebay Alerts to your inbox

  3. #3
    D-Mac's Avatar
    Registered User

    Status
    Offline
    Join Date
    Mar 2004
    Location
    Surrey
    Posts
    1,353
    Thanks
    29
    Thanked 49 Times in 44 Posts
    make sure you're not storing the £ symbol in the database, and set the price field to decimal
    David Macfarlane
    Cost effective web development. Codewise

  4. #4
    Registered User

    Status
    Offline
    Join Date
    Sep 2003
    Posts
    315
    Thanks
    28
    Thanked 19 Times in 16 Posts
    Hi Guys, thanks for the posts. I think I've probably explained this the wrong way.

    Here's my actual SQL query on this one:

    SELECT MIN(o.price_with_delivery_and_voucher), o.merchant_name,
    p.page, p.description, p.brand_name, p.product_name, p.specs, p.model
    FROM offers o JOIN products p
    ON o.product_name = p.product_name
    WHERE p.cat1 = 'Washer Dryers'
    AND o.display_price > 0
    AND o.in_stock = 'true'
    AND o.price_with_delivery_and_voucher > 400 GROUP BY o.product_name
    ORDER BY o.price_with_delivery_and_voucher ASC LIMIT 0, 10

    Here's the problem I'm having.

    When I use the GROUP, it groups everything together and I don't want that. I want the lowest price item, but I want the lowest price to be over 400 in this example.

    Thanks!

    Tom

  5. #5
    Registered User

    Status
    Offline
    Join Date
    Sep 2003
    Posts
    315
    Thanks
    28
    Thanked 19 Times in 16 Posts
    Aha! After a frustrating day yesterday, I think I've nailed it with a subquery...

    SELECT *
    FROM (
    SELECT product_name, MIN(price_with_delivery_and_voucher) AS best_price
    FROM offers WHERE cat1 = 'Cookers'
    AND in_stock = 'true'
    AND display_price > 0
    GROUP BY product_name
    ORDER BY best_price ASC
    ) AS myalias
    WHERE best_price > 500

  6. #6
    scriptmonkey's Avatar
    Oranges & Lemons

    Status
    Offline
    Join Date
    Jan 2009
    Location
    Worthing
    Posts
    1,607
    Thanks
    112
    Thanked 253 Times in 192 Posts
    Try using 'HAVING' insted, so:
    PHP Code:
    GROUP BY o.price_with_delivery_and_voucher ASC HAVING o.price_with_delivery_and_voucher 400 LIMIT 010

    should replace these
    :

    AND 
    o.price_with_delivery_and_voucher 400 GROUP BY o.product_name
    ORDER BY o
    .price_with_delivery_and_voucher ASC LIMIT 010 
    The trouble with the rat race is that even if you win you're still a rat.
    Time passes. Listen. Time passes. Dylan Thomas
    Ebay Alerts to your inbox



Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
To Top

Content Relevant URLs by vBSEO 3.5.0 RC2