根据个人搜索条件获取通过特定摄像头的车辆

5gfr0r5j  于 2021-06-24  发布在  Mysql
关注(0)|答案(3)|浏览(233)

我的问题与此问题相关,但它不同,因为每个搜索筛选器的日期和时间不同。
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');

要求:
用户有两个搜索筛选器。
在第一个搜索过滤器,他说,让我所有的汽车有sourceid在(44,3)的特定日期和时间。假设(e.createdon>'2016-09-20 20:24:00'和e.createdon<'2016-09-20 20:35:00')
在第二个搜索过滤器,他说,让我所有的汽车有sourceid在(26,3,5)为另一个日期和时间。假设(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版本:
马里亚布-5.5.50
mysql-5.5.51版本

vjrehmav

vjrehmav1#

我用下面的查询来解决我的问题。希望它能帮助别人。

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;
ckx4rj1h

ckx4rj1h2#

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;
eqfvzcg8

eqfvzcg83#

从你的病情来看,这是我的猜测

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

相关问题