我的问题与this问题有关,但因为每个搜索过滤器的日期时间不同而有所不同。
MYSQL / MARIADB架构和示例数据:
CREATE DATABASE IF NOT EXISTS `puzzle` DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_unicode_ci;
USE `puzzle`;
DROP TABLE IF EXISTS `event`;
CREATE TABLE `event` (
`eventId` bigint(20) NOT NULL AUTO_INCREMENT,
`sourceId` bigint(20) NOT NULL COMMENT 'think of source as camera',
`carCode` varchar(40) NOT NULL COMMENT 'ex: A',
`carNumber` varchar(40) NOT NULL COMMENT 'ex: 5849',
`createdOn` datetime DEFAULT NULL,
PRIMARY KEY (`eventId`)
) ENGINE=INNODB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
INSERT INTO `event` (`eventId`, `sourceId`, `carCode`, `carNumber`, `createdOn`) VALUES
(1, 44,'A', '4456', '2016-09-20 20:24:05'),
(2, 26,'B', '26484', '2016-09-20 20:24:05'),
(3, 5,'A', '4456', '2016-09-20 20:24:06'),
(4, 3,'C', '72704', '2016-09-20 20:24:15'),
(5, 3,'D' ,'399606', '2016-09-20 20:26:15'),
(6, 5, 'A', '4456', '2016-09-20 20:27:25'),
(7, 44,'C', '72704', '2016-09-20 20:29:25'),
(8, 3,'A' ,'4456', '2016-09-20 20:30:55'),
(9, 44,'B' ,'26484', '2016-09-20 20:34:55'),
(10, 26,'B' ,'4456', '2016-09-20 20:35:15'),
(11, 3, 'C','72704', '2016-09-20 20:35:15'),
(12, 3,'D', '399606', '2016-09-20 20:44:35'),
(13, 26,'A' ,'4456', '2016-09-20 20:49:45');
要求:
用户有两个搜索过滤器。
在他说的第一个搜索过滤器中,给我所有在(44,3)中有sourceId的汽车在某个日期和时间。假设(e.createdOn>' 2016-09-20 20:24:00' e.createdOn<""" 2016-09-20 20:35:00')
在第二个搜索过滤器中,他说,给我所有在(26,3,5)中有sourceId的汽车换另一个日期和时间。假设(e.createdOn>' 2016-09-20 20:36:00'和e.createdOn<""" 2016-09-20 20:49:00')
现在我想获得两个搜索结果中都有的所有(carNumbers,carCode)。
查询需要很快,因为真实表包含超过3亿条记录。
到目前为止,我可以使用查询的最大值(它甚至不能产生有效结果)
select distinct e1.carNumber, e1.carCode from event e1
inner join (select e2.carNumber, e2.carCode from event e2 where e2.sourceId in (44,3) ) e3 on e1.carNumber= e3.carNumber
inner join (select e2.carNumber, e2.carCode from event e2 where e2.sourceId in (44,3) ) e4 on e1.carCode= e4.carCode
inner join (select e2.carNumber, e2.carCode from event e2 where e2.sourceId in (26,3,5) ) e5 on e1.carNumber= e5.carNumber
inner join (select e2.carNumber, e2.carCode from event e2 where e2.sourceId in (26,3,5) ) e6 on e1.carCode= e6.carCode
我想知道SQL是否能够解决这个问题,还是应该使用后端编码?
正在使用的MySQL / MariaDB版本:
MariaDB的-50年5月5日
的MySQL-51年5月5日
答案 0 :(得分:0)
这是我从你的病情读数中猜到的
select distinct e1.carNumber, e1.carCode from event e1
where e.sourceId in (44,3)
and e.createdOn = "certain date"
and exists (
select 'x' from event e2
where e2.sourceId in (26,3,5)
and e2.createdOn = "another date"
and e1.carCode = e2.carCode
and e1.carNumber = e2.carNumber
)
从第二个过滤器的选择中返回第一个过滤器中的所有汽车
一次阅读一部分:
select distinct e1.carNumber, e1.carCode from event e1
where e.sourceId in (44,3)
and e.createdOn = "certain date"
将从第一个过滤器返回所有汽车。由此,我们需要再次使用第二个过滤。
select 'x' from event e2
where e2.sourceId in (26,3,5)
and e2.createdOn = "another date"
and e1.carCode = e2.carCode
and e1.carNumber = e2.carNumber
我使用exists
子句检查第一个结果中是否存在汽车的结果。
编辑2:上次编辑,因为您不断添加需求...
select distinct e1.carNumber, e1.carCode from event e1
inner join event e2 on e1.carCode = e2.carCode and e1.carNumber = e2.carNumber and e2.sourceId in (26,3,5) and e2.createdOn = "another date" --the second filter
-- inner join event e3 on e1.carCode = e3.carCode and e1.carNumber = e3.carNumber and e3.sourceId in (x,y,z) and e3.createdOn = "some crazy date" --the third filter ...
where e.sourceId in (44,3) and e.createdOn = "certain date" --the first filter
答案 1 :(得分:0)
E.g。 (可能不完全是你所追求的)
SELECT carnumber
FROM event
WHERE (sourceid IN (44,3) AND createdon BETWEEN '2016-09-20 20:24:00' and '2016-09-20 20:35:00')
OR (sourceid IN (26,3,5) AND createdon BETWEEN '2016-09-20 20:36:00' and '2016-09-20 20:49:00')
GROUP
BY carnumber HAVING COUNT(*) > 1;
答案 2 :(得分:0)
我用下面的查询来解决我的问题。希望它能帮助别人。
SELECT e1.carCode, e1.carNumber from event e1
WHERE (sourceid IN (44,3) AND createdon BETWEEN '2016-09-20 20:24:00' and '2016-09-20 20:35:00')
OR (sourceid IN (26,3,5) AND createdon BETWEEN '2016-09-20 20:36:00' and '2016-09-20 20:49:00')
group by e1.carCode, e1.carNumber
having sum(e1.sourceId IN (44,3)) > 0 and
sum(e1.sourceId IN (26,3,5)) > 0;