Search code examples
mysqlsqlsubquerylocalizednotin

SQL NOT IN Query


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?


Solution

  • 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