Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/148.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 两行数据之间的匹配_Mysql_Sql_Match - Fatal编程技术网

Mysql 两行数据之间的匹配

Mysql 两行数据之间的匹配,mysql,sql,match,Mysql,Sql,Match,我想从结果中选择匹配计数,其中匹配计数是日期上的精确整数 date id_event id_timewindows max_hits 2014-12-16 1 1,2,3 2 2014-12-16 2 2,3,4 2 2014-12-16 3 4 2 2014-12-16 4 5,6

我想从结果中选择匹配计数,其中匹配计数是日期上的精确整数

date        id_event    id_timewindows      max_hits
2014-12-16  1           1,2,3               2
2014-12-16  2           2,3,4               2
2014-12-16  3           4                   2
2014-12-16  4           5,6                 2
2014-12-16  5           7,8                 2
2014-12-16  6           9                   2
我想要的结果是:

date        id_event    id_timewindows      max_hits
2014-12-16  1           2,3                 2
2014-12-16  2           2,3                 2
有人知道如何在MySQL中实现吗

更新: 所以我必须解释更多。
id\u时间窗口
不是一个
字符串
属性,第一个属性是视图的结果,该视图按id\u事件分组,
一个id\u事件有多个id\u时间窗口

分组前查看结果:

date        id_event    id_timewindow       begin       end         max_rooms
2014-12-16  1           1                   06:00:00    07:00:00    2
2014-12-16  1           2                   07:00:00    08:00:00    2
2014-12-16  1           3                   08:00:00    09:00:00    2
2014-12-16  2           2                   07:00:00    08:00:00    2
2014-12-16  2           3                   08:00:00    09:00:00    2
2014-12-16  2           4                   09:00:00    10:00:00    2
2014-12-16  3           4                   09:00:00    10:00:00    2
2014-12-16  4           6                   11:00:00    12:00:00    2
2014-12-16  4           5                   10:00:00    11:00:00    2
2014-12-16  5           7                   12:00:00    13:00:00    2
2014-12-16  5           8                   13:00:00    14:00:00    2
2014-12-16  6           9                   14:00:00    15:00:00    2
我使用
GROUP BY id\u event
,而
id\u timewindows
GROUP\u concat(id\u timewindow SEPARATOR',)
我找到了一个解决方案:

SELECT 
   date,
   id_timewindow,
   max_rooms,
   COUNT(concat(date, id_timewindow)) as counter

FROM `timewindows_reserved`
GROUP BY
   date, id_timewindow

HAVING counter < max_rooms
如果我按日期分组并从
id\u timewindow
中列出一个列表,那么我可以收到与我想要的相同的结果,但逻辑相反。不是保留的时间窗口,而是空闲时间窗口。如果我将其反转,则可以得到结果:

SELECT 
   date,
   id_event,
   GROUP_CONCAT(id_timewindow SEPARATOR ','),
   max_rooms,
   COUNT(concat(date, id_timewindow)) as counter

FROM `table`
GROUP BY
   date, id_timewindow

HAVING counter >= max_rooms

我不明白你是怎么得到结果的。
SELECT 
   date,
   id_event,
   GROUP_CONCAT(id_timewindow SEPARATOR ','),
   max_rooms,
   COUNT(concat(date, id_timewindow)) as counter

FROM `table`
GROUP BY
   date, id_timewindow

HAVING counter >= max_rooms