mysql查询以查找销量少于10件且在发布后的7天内至少有1次销售的产品。

huangapple go评论72阅读模式
英文:

mysql query for finding the products that had less than 10 sales and at least 1 sale within 7 days after their release

问题

以下是我的表格:

  1. 产品表:
CREATE TABLE `products` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `name` varchar(255) NOT NULL,
  `description` text NOT NULL,
  `price` decimal(15,2) NOT NULL,
  `total_profit` decimal(15,2) NOT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
  1. 订单表:
CREATE TABLE `orders` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `user_id` bigint(20) UNSIGNED NOT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
  1. 订单详情表:
CREATE TABLE `order_details` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `order_id` bigint(20) UNSIGNED NOT NULL,
  `product_id` bigint(20) UNSIGNED NOT NULL,
  `quantity` int(11) UNSIGNED NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

我想要一个查询,获取销量少于10件且发布后7天内至少有1次销售的产品的ID,并按产品ID升序排序。

我正在尝试以下代码,但它只返回连接表的第一行。我认为它没有正确计数!

SELECT p.id
FROM orders o
JOIN order_details od ON o.id = od.order_id
JOIN products p ON od.product_id = p.id
WHERE timestampdiff(day, o.created_at, p.created_at) <= 7
HAVING COUNT(od.quantity) BETWEEN 1 AND 10
ORDER BY p.id ASC;

以下是示例数据:

INSERT INTO `products` (`id`, `name`, `description`, `price`, `total_profit`, `created_at`) VALUES
(1, 'consequatur dolores dolorum', 'Amet iste.', '10899010.00', '0.00', '2022-04-05 18:47:19'),
(2, 'est dolor dolores', 'Minus.', '36014000.00', '0.00', '2022-04-06 18:47:19'),
(3, 'quibusdam sed vel', 'A quo sed.', '32255000.00', '0.00', '2022-04-07 18:47:19'),
(4, 'perferendis dolores molestias', 'Ipsam sit.', '10182000.00', '0.00', '2022-04-08 18:47:19'),
(5, 'fuga aspernatur natus', 'Earum quas.', '737000.00', '0.00', '2022-04-09 18:47:19'),
(6, 'voluptatibus incidunt nostrum', 'Quia possimus.', '36728000.00', '0.00', '2022-04-10 18:47:19'),
(7, 'et necessitatibus architecto', 'Aut.', '43860000.00', '0.00', '2022-04-11 18:47:19'),
(8, 'et id nisi', 'Qui id totam.', '10380000.00', '0.00', '2022-04-12 18:47:19'),
(9, 'ipsam ut iusto', 'Iusto.', '21780000.00', '0.00', '2022-04-13 18:47:19'),
(10, 'similique accusantium et', 'A qui ducimus.', '23156000.00', '0.00', '2022-04-14 18:47:19');

INSERT INTO `order_details` (`id`, `order_id`, `product_id`, `quantity`) VALUES
(1, 2, 4, 8),
(2, 10, 49, 3),
(3, 6, 12, 8),
(4, 8, 3, 9),
(5, 9, 75, 7),
(6, 5, 4, 10),
(7, 7, 100, 7),
(8, 2, 63, 1),
(9, 1, 21, 2),
(10, 1, 79, 9),
(11, 4, 10, 6);

INSERT INTO `orders` (`id`, `user_id`, `created_at`) VALUES
(1, 217003, '2022-07-09 18:47:19'),
(2, 732493, '2022-07-10 06:47:19'),
(3, 269127, '2022-07-10 18:47:19'),
(4, 47245, '2022-07-11 06:47:19'),
(5, 207792, '2022-07-11 18:47:19'),
(6, 456178, '2022-07-12 06:47:19'),
(7, 875098, '2022-07-12 18:47:19'),
(8, 353625, '2022-07-13 06:47:19'),
(9, 258266, '2022-07-13 18:47:19'),
(10, 474065, '2022-07-14 06:47:19');
英文:

here are my tables:

  1. products table:
CREATE TABLE `products` (
`id` bigint(20) UNSIGNED NOT NULL,
`name` varchar(255) NOT NULL,
`description` text NOT NULL,
`price` decimal(15,2) NOT NULL,
`total_profit` decimal(15,2) NOT NULL,
`created_at` timestamp NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
  1. orders table:
CREATE TABLE `orders` (
`id` bigint(20) UNSIGNED NOT NULL,
`user_id` bigint(20) UNSIGNED NOT NULL,
`created_at` timestamp NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
  1. order_details table:
CREATE TABLE `order_details` (
`id` bigint(20) UNSIGNED NOT NULL,
`order_id` bigint(20) UNSIGNED NOT NULL,
`product_id` bigint(20) UNSIGNED NOT NULL,
`quantity` int(11) UNSIGNED NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

what i want is a query that gets the ID of the products that had less than 10 sales and at least 1 sale within 7 days after their release, in ascending order of the product ID.

I'm trying this code. and it only return the first row of the joined table. i think it dosen't count correctly!

SELECT p.id
FROM orders o
JOIN order_details od ON o.id = od.order_id
JOIN products p ON od.product_id = p.id
where timestampdiff(day, o.created_at, p.created_at) &lt;= 7
having count(od.quantity)  between 0 and 10
order BY p.id asc;

here is sample data:

INSERT INTO `products` (`id`, `name`, `description`, `price`, `total_profit`, `created_at`) VALUES
(1, &#39;consequatur dolores dolorum&#39;, &#39;Amet iste.&#39;, &#39;10899010.00&#39;, &#39;0.00&#39;, &#39;2022-04-05 18:47:19&#39;),
(2, &#39;est dolor dolores&#39;, &#39;Minus.&#39;, &#39;36014000.00&#39;, &#39;0.00&#39;, &#39;2022-04-06 18:47:19&#39;),
(3, &#39;quibusdam sed vel&#39;, &#39;A quo sed.&#39;, &#39;32255000.00&#39;, &#39;0.00&#39;, &#39;2022-04-07 18:47:19&#39;),
(4, &#39;perferendis dolores molestias&#39;, &#39;Ipsam sit.&#39;, &#39;10182000.00&#39;, &#39;0.00&#39;, &#39;2022-04-08 18:47:19&#39;),
(5, &#39;fuga aspernatur natus&#39;, &#39;Earum quas.&#39;, &#39;737000.00&#39;, &#39;0.00&#39;, &#39;2022-04-09 18:47:19&#39;),
(6, &#39;voluptatibus incidunt nostrum&#39;, &#39;Quia possimus.&#39;, &#39;36728000.00&#39;, &#39;0.00&#39;, &#39;2022-04-10 18:47:19&#39;),
(7, &#39;et necessitatibus architecto&#39;, &#39;Aut.&#39;, &#39;43860000.00&#39;, &#39;0.00&#39;, &#39;2022-04-11 18:47:19&#39;),
(8, &#39;et id nisi&#39;, &#39;Qui id totam.&#39;, &#39;10380000.00&#39;, &#39;0.00&#39;, &#39;2022-04-12 18:47:19&#39;),
(9, &#39;ipsam ut iusto&#39;, &#39;Iusto.&#39;, &#39;21780000.00&#39;, &#39;0.00&#39;, &#39;2022-04-13 18:47:19&#39;),
(10, &#39;similique accusantium et&#39;, &#39;A qui ducimus.&#39;, &#39;23156000.00&#39;, &#39;0.00&#39;, &#39;2022-04-14 18:47:19&#39;),
INSERT INTO `order_details` (`id`, `order_id`, `product_id`, `quantity`) VALUES
(1, 2, 4, 8),
(2, 10, 49, 3),
(3, 6, 12, 8),
(4, 8, 3, 9),
(5, 9, 75, 7),
(6, 5, 4, 10),
(7, 7, 100, 7),
(8, 2, 63, 1),
(9, 1, 21, 2),
(10, 1, 79, 9),
(11, 4, 10, 6),
INSERT INTO `orders` (`id`, `user_id`, `created_at`) VALUES
(1, 217003, &#39;2022-07-09 18:47:19&#39;),
(2, 732493, &#39;2022-07-10 06:47:19&#39;),
(3, 269127, &#39;2022-07-10 18:47:19&#39;),
(4, 47245, &#39;2022-07-11 06:47:19&#39;),
(5, 207792, &#39;2022-07-11 18:47:19&#39;),
(6, 456178, &#39;2022-07-12 06:47:19&#39;),
(7, 875098, &#39;2022-07-12 18:47:19&#39;),
(8, 353625, &#39;2022-07-13 06:47:19&#39;),
(9, 258266, &#39;2022-07-13 18:47:19&#39;),
(10, 474065, &#39;2022-07-14 06:47:19&#39;);

答案1

得分: 0

我认为你只是忘了加上 GROUP BY

SELECT 
p.`id`, 
SUM(od.`quantity`) totalSold 
FROM orders o
JOIN order_details od ON o.`id`=od.`order_id`
JOIN `products` p ON od.`product_id`=p.`id`
WHERE TIMESTAMPDIFF(DAY, o.created_at, p.created_at) <= 7
GROUP BY p.`id`
HAVING totalSold < 10

而且你的条件是检查“小于 10”,我不明白为什么你写了“0 AND 100”。

英文:

I think you are just missing a GROUP BY:

SELECT 
p.`id`, 
SUM(od.`quantity`) totalSold 
FROM orders o
JOIN order_details od ON o.`id`=od.`order_id`
JOIN `products` p ON od.`product_id`=p.`id`
WHERE TIMESTAMPDIFF(DAY, o.created_at, p.created_at) &lt;= 7
GROUP BY p.`id`
HAVING totalSold&lt;10

And also your condition is to check LESS THAN 10, I do not understand why you wrote 0 AND 100

答案2

得分: 0

我稍微修改了@Asgar的答案,这是关键!以下是最终答案:

SELECT 
p.`id`
FROM orders o
JOIN order_details od ON o.`id` = od.`order_id`
JOIN `products` p ON od.`product_id` = p.`id`
WHERE TIMESTAMPDIFF(DAY, p.created_at, o.created_at) <= 7
GROUP BY p.`id`
HAVING sum(od.quantity) between 1 and 9
ORDER BY p.id ASC

我也感谢@Barmar。

英文:

I changed @Asgar 's answer a little and it was the key!
here is the final asnwer:

SELECT 
p.`id`
FROM orders o
JOIN order_details od ON o.`id` = od.`order_id`
JOIN `products` p ON od.`product_id` = p.`id`
WHERE TIMESTAMPDIFF(DAY, p.created_at, o.created_at) &lt;= 7
GROUP BY p.`id`
HAVING sum(od.quantity) between 1 and 9
order by p.id asc

I also thank @Barmar.

huangapple
  • 本文由 发表于 2023年7月20日 20:47:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76730067.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定