过去7天没有销售的产品

时间:2019-05-10 11:52:29

标签: mysql sql

我有两个表产品和user_sales。我需要获取最近7天销售量为0的产品。

我尝试使用“ HAVING”语句在堆栈溢出中找到了一些代码,但是我发现是否限制了12种产品,而不是不起作用。

Anoyone可以帮忙吗?

table products

id | title | price | images | description

table user_sales

id | product_id | sale_date | user_owner

那是表的简化版本。实际的查询看起来像这样。

SELECT `product_store`.`friendly_name`        AS `store_friendly_name`, 
       `api_keys`.`key_data`                  AS `api_data`, 
       `ep`.*, 
       `supplier_store`.`icon`                AS `supplier_store_icon`, 
       `supplier_store`.`internal_name`       AS `supplier_store_internal_name`, 
       `supplier_store`.`friendly_name`       AS `supplier_store_friendly_name`, 
       `supplier_store`.`amazon_type_product` AS 
       `supplier_store_amazon_type_product`, 
       `supplier_store`.`handler`             AS `supplier_store_handler`, 
       `sp`.`extra_data`                      AS `sp_extra_data`, 
       `sp`.`id`                              AS `sp_id`, 
       `sp`.`remote_id`                       AS `sp_remote_id`, 
       `sp`.`url`                             AS `sp_url`, 
       `sp`.`price`                           AS `sp_price`, 
       `sp`.`stock`                           AS `sp_stock`, 
       `sp`.`picture`                         AS `sp_picture`, 
       `sp`.`store_id`                        AS `sp_store_id`, 
       Count(us.date)                         AS sale_date 
FROM   `products` `ep` 
       LEFT JOIN `stores` `product_store` 
              ON `ep`.`store_id` = `product_store`.`id` 
       LEFT JOIN `api_keys` 
              ON `api_keys`.`id` = `ep`.`link_key` 
       LEFT JOIN `products` `sp` 
              ON `ep`.`linked_to` = `sp`.`id` 
       LEFT JOIN `stores` `supplier_store` 
              ON `supplier_store`.`id` = `sp`.`store_id` 
       RIGHT JOIN `user_sales` `us` 
               ON `ep`.`remote_id` = `us`.`remote_id` 
WHERE  `ep`.`user_owner` = '3992' 
       AND `ep`.`expired` = 0 
       AND `us`.`user_id` = '3992' 
       AND us.date > "2019-02-09 14:21:34" 
       AND us.date < "2019-05-10 14:21:34" 
       AND `ep`.`store_id` = 3 
GROUP  BY `ep`.`id` 
HAVING `sale_date` < 1 
ORDER  BY `ep`.`id` DESC 
LIMIT  15 

1 个答案:

答案 0 :(得分:0)

您要确保最近7天该产品不存在任何销售。为此使用NOT EXISTS

根据您的查询,我认为您希望将其限制为属于user_owner 3992的产品以及同一用户的销售。

WHERE ep.user_owner = 3992
AND ep.expired = 0 
AND ep.store_id = 3 
AND NOT EXISTS
(
  SELECT *
  FROM user_sales us
  WHERE us.user_id = ep.user_owner
  AND us.date > current_date - interval 7 day
)

完整查询:

SELECT product_store.friendly_name         AS store_friendly_name, 
       api_keys.key_data                   AS api_data, 
       ep.*,
       supplier_store.icon                 AS supplier_store_icon, 
       supplier_store.internal_name        AS supplier_store_internal_name, 
       supplier_store.friendly_name        AS supplier_store_friendly_name, 
       supplier_store.amazon_type_product  AS 
       supplier_store_amazon_type_product, 
       supplier_store.handler              AS supplier_store_handler, 
       sp.extra_data                       AS sp_extra_data, 
       sp.id                               AS sp_id, 
       sp.remote_id                        AS sp_remote_id, 
       sp.url                              AS sp_url, 
       sp.price                            AS sp_price, 
       sp.stock                            AS sp_stock, 
       sp.picture                          AS sp_picture, 
       sp.store_id                         AS sp_store_id
FROM products ep 
LEFT JOIN stores product_store ON ep.store_id = product_store.id 
LEFT JOIN api_keys ON api_keys.id = ep.link_key 
LEFT JOIN products sp ON ep.linked_to = sp.id 
LEFT JOIN stores supplier_store ON supplier_store.id = sp.store_id 
WHERE ep.user_owner = 3992
AND ep.expired = 0 
AND ep.store_id = 3 
AND NOT EXISTS
(
  SELECT *
  FROM user_sales us
  WHERE us.user_id = ep.user_owner
  AND us.date > current_date - interval 7 day
)
ORDER  BY ep.id DESC;