按当前列和时间计算行数

时间:2018-04-05 11:10:04

标签: mysql

我的查询有问题。我想在特定时间计算给定列中特定短语的出现次数。

这是我的表:

CREATE TABLE `table` (
  `id` int(11) NOT NULL,
  `prodId` int(11) NOT NULL,
  `bookingAt` datetime NOT NULL,
  `equipmentType` varchar(30) NOT NULL,
  `shippingAt` datetime DEFAULT NULL
)

我有一个查询库存收货数量的工作查询:

SELECT g1.equipmentType,COUNT(*) as count FROM table g1
WHERE g1.shippingAt IS NULL AND 
g1.bookingAt BETWEEN :from AND :to GROUP BY g1.equipmentType

但我不知道如何为发送的产品构建查询:( 表中的示例数据:

INSERT INTO `table` (`id`, `prodId`, `bookingAt`, `equipmentType`, `shippingAt`) VALUES
(1, 9911199, '2018-04-05 09:30:03', 'Type1','2018-04-05 14:00:00'),
(2, 9911100, '2018-04-05 08:00:00', 'Type1','2018-04-05 14:10:00'),
(3, 1234563, '2018-04-05 10:05:40', 'Type2', NULL);

我的查询的工作结果(预订):

array [
  0 => array [
    "equipmentType" => "Type2"
    "count" => "1"
  ]

预期结果(发货):

array [
  0 => array [
    "equipmentType" => "Type1"
    "count" => "2"
  ]

1 个答案:

答案 0 :(得分:0)

添加' NOT'在shippingAt上测试NULL会给你发货的产品。

SELECT g1.equipmentType,COUNT(*) as count 
FROM table g1
WHERE g1.shippingAt IS NOT NULL 
AND g1.bookingAt BETWEEN :from AND :to 
GROUP BY g1.equipmentType;

此查询基于您提供的表格和数据,但与标题或您在问题开头时所询问的内容之间的关系是一个谜。