SQL 不在查询中

发布于 2024-08-11 23:25:34 字数 886 浏览 9 评论 0原文

谁能帮我解决这个 MySQL 查询?

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

我正在尝试加入过去 30 天内 ProductID 不在实时馈送表中以及过去 6 小时内 storeid 不在实时馈送表中的位置,但它似乎不起作用。知道我做错了什么吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

原谅我要高飞 2024-08-18 23:25:34

乍一看,您的第一个子查询应该选择 ProductID,而不是 LiveFeedID,第二个子查询应该选择 StoreID 而不是 >LiveFeedID

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

岁月流歌 2024-08-18 23:25:34

我太晚了:

  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

您正在将 EXISTS 语法与相关子查询一起使用......

我正在尝试为每家商店获得最高折扣。

在这种情况下,请使用:

  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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文