|
我使用库存数据,每分钟告诉我当前的库存水平,并将其存储在数据库中。
8 D9 m S. L' d( a Z& N; x t; X我想查找item_count降至0的每个实例,带时间戳的那一行,然后将其加入到item_count下一行高于0。然后,这将告诉我产品缺货多久。
- @' Y% c# u: `. |7 d我想出了以下内容,但没有返回任何内容。9 ^$ }- r; ]$ {: O. x3 J4 T
SELECT `inventories`.* from `inventories` inner join (SELECT id,item_count,pusher_id,created_at as in_stock_at FROM inventories GROUP BY pusher_id) inv2 ON `inventories`.`created_at` `inventories`.`item_count` AND `inventories`.`pusher_id` = `inv2`.`pusher_id` WHERE `inventories`.`item_count` 结构::
' @ ?- ?0 ^ M2 e5 `3 uCREATE TABLE IF NOT EXISTS `inventories` ( `id` int(10) unsigned NOT NULL, `client_id` int(10) unsigned NOT NULL, `pusher_id` int(10) unsigned NOT NULL, `product_id` int(10) unsigned NOT NULL, `reader_id` int(10) unsigned NOT NULL, `tags_blocked` double(6,2) NOT NULL, `item_count` double(6,2) NOT NULL, `active` tinyint(1) NOT NULL DEFAULT '1', `created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00', `updated_at` timestamp NOT NULL DEFAULT '0000-00- `deleted_at` timestamp NULL DEFAULT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=2881 ;数据::3 t0 g& ~6 A) ^0 `1 Y
INSERT INTO `inventories` (`id`,`client_id`,`pusher_id`,`product_id`,`reader_id`,`tags_blocked`,`item_count`,`active`,`created_at`,`updated_at`,`deleted_at`) VALUES .00,0.00,1,'2015-10-22 04:45:47,2015-10-23 04:45:47',NULL), (2,1,1,9,1,0.00,0.00,1,'2015-10-22 04:55:47,2015-10-23 04:45:47',NULL), .00,0.00,1,'2015-10-22 05:05:47,2015-10-23 04:45:47',NULL), ... 1,1,9,1,0.00,0.00,1,'2015-10-22 06:15:47,2015-10-23 04:45:47',NULL), .00,10.00,1,'2015-10-22 06:25:47,2015-10-23 04:45:47',NULL), .00,9.00,1,'2015-10-22 06:35:47,2015-10-23 04:45:47',NULL), .00,8.00,1,'2015-10-22 06:45:47,2015-10-23 04:45:47',NULL),所需结果::& L$ k/ r4 g: H; d0 @: z
给出上述数据,我想ID为1的行和ID将11行连接在一起。.在表中搜索item_count = 0 0的第一行item_count>! ^: L; ^: _2 }# `8 r m
0和created_at有相同的行product_id和pusher_id) >& o0 {- l# L8 e# k0 G, ]* m
firstRow.created_at。并加入他们的行列。然后,找到事件的下一个例子。
% b; R2 l# \( h) I" o我希望这个问题能得到澄清。
5 B3 y$ o% Y2 P0 D% ?$ K. I ) i* ^+ ?" A' ]
解决方案: |
|