views:

106

answers:

2

Can anyone help me with this MySQL query?

SELECT p.ProductID, 
       p.StoreID, 
       p.DiscountPercentage 
 FROM Products p 
WHERE p.IsSpecial = 1 
  AND p.SpecialDate >= date_sub(now(),interval 15 minute) 
  AND p.DiscountPercentage >= ?DiscountPercentage 
  AND p.ProductID NOT IN (SELECT lf.LiveFeedID 
                            From LiveFeed lf 
                           WHERE p.ProductID = lf.ProductID 
                             AND lf.DateAdded >= date_sub(now(),interval 30 day)) 
  AND p.StoreID NOT IN (SELECT lf.LiveFeedID 
                          From LiveFeed lf 
                         WHERE p.StoreID = lf.StoreID 
                           AND lf.DateAdded >= date_sub(now(),interval 6 hour)) 
ORDER BY p.StoreID, p.DiscountPercentage DESC

I'm trying join where the ProductID is not in the livefeed table in the last 30 days and where the storeid is not in the livefeed table in the last 6 hours, but it does not seem to be working. Any idea what I'm doing wrong?

+3  A: 

At a glance, it would appear that your first subquery should be selecting ProductID, not LiveFeedID and your second subquery should be selecting StoreID not LiveFeedID

kristian
ahhh you are correct! I knew it was something simple that I was missing! Thanks!
mike
+2  A: 

I'm too late:

  SELECT p.ProductID, 
         p.StoreID, 
         p.DiscountPercentage 
    FROM Products p 
   WHERE p.IsSpecial = 1 
     AND p.SpecialDate >= date_sub(now(),interval 15 minute) 
     AND p.DiscountPercentage >= ?DiscountPercentage 
     AND p.ProductID NOT IN (SELECT lf.productid
                               FROM LIVEFEED lf
                              WHERE lf.DateAdded >= DATE_SUB(NOW(), INTERVAL 30 DAY)) 
     AND p.storeid NOT IN (SELECT lf.storeid
                             FROM LIVEFEED lf
                            WHERE lf.DateAdded >= DATE_SUB(NOW(), INTERVAL 6 HOUR)) 
ORDER BY p.StoreID, p.DiscountPercentage DESC

You were using EXISTS syntax with a correllated subquery...

I'm trying to get the top discount for each store.

In that case, use:

  SELECT p.StoreID, 
         MAX(p.DiscountPercentage)
    FROM Products p 
   WHERE p.IsSpecial = 1 
     AND p.SpecialDate >= date_sub(now(),interval 15 minute) 
     AND p.DiscountPercentage >= ?DiscountPercentage 
     AND p.ProductID NOT IN (SELECT lf.productid
                               FROM LIVEFEED lf
                              WHERE lf.DateAdded >= DATE_SUB(NOW(), INTERVAL 30 DAY)) 
     AND p.storeid NOT IN (SELECT lf.storeid
                             FROM LIVEFEED lf
                            WHERE lf.DateAdded >= DATE_SUB(NOW(), INTERVAL 6 HOUR)) 
GROUP BY p.storeid
ORDER BY p.StoreID, p.DiscountPercentage DESC
OMG Ponies
Thank you! Is there a simple way to only allow one storeID in the query?
mike
Only 1? Tack `LIMIT 1` on to the end. But I'd need to know more about how to determine which specific storeid to return...
OMG Ponies
Well, as the query is now, it will return multiple product id's from the same store. I need a limit of 1 for each storeid within the query. I'm trying to get the top discount for each store.
mike