英文:
mysql query for finding the products that had less than 10 sales and at least 1 sale within 7 days after their release
问题
以下是我的表格:
- 产品表:
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;
- 订单表:
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;
- 订单详情表:
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:
- 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;
- 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;
- 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) <= 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, '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');
答案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) <= 7
GROUP BY p.`id`
HAVING totalSold<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) <= 7
GROUP BY p.`id`
HAVING sum(od.quantity) between 1 and 9
order by p.id asc
I also thank @Barmar.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论