Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/58.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**为每个GroupId选择前后行_Mysql_Sql_Row_Unix Timestamp_Before Filter - Fatal编程技术网

**MySql**为每个GroupId选择前后行

**MySql**为每个GroupId选择前后行,mysql,sql,row,unix-timestamp,before-filter,Mysql,Sql,Row,Unix Timestamp,Before Filter,如何为每个grp_id设置2行: 在当前unix时间戳的行之前和之后 +---------+--------------------+------------+-------+ | id | grp_id | utimes | value | +---------+--------------------+------------+-------+ | 4156187 | 5282 | 1455663600 | 15897 |

如何为每个grp_id设置2行: 在当前unix时间戳的行之前和之后

+---------+--------------------+------------+-------+ | id | grp_id | utimes | value | +---------+--------------------+------------+-------+ | 4156187 | 5282 | 1455663600 | 15897 | | 4159888 | 5282 | 1455630000 | 26998 |* | 4156190 | 5282 | 1455676200 | 28497 | | 4156186 | 5282 | 1455661800 | 14097 | | 4156183 | 5282 | 1455652800 | 5097 | | 4156184 | 5282 | 1455656400 | 8697 | | 4156185 | 5282 | 1455660000 | 12297 | | 4156182 | 5282 | 1455651000 | 3297 |* | 4163311 | 7216 | 1455693000 | 45297 | | 4163275 | 7203 | 1455681600 | 33897 | | 4163309 | 7214 | 1455697800 | 50097 | | 4163308 | 7214 | 1455696000 | 48297 | | 4163307 | 7214 | 1455694200 | 46497 | | 4163306 | 7214 | 1455692400 | 44697 | | 4163305 | 7214 | 1455690600 | 42897 | | 4163304 | 7214 | 1455688800 | 41097 | | 4151121 | 4356 | 1455703200 | 55497 | | 4163271 | 7205 | 1455685500 | 37797 | | 4163272 | 7205 | 1455687000 | 39297 | | 4163269 | 7205 | 1455684900 | 37197 | | 4163273 | 7205 | 1455687300 | 39597 | | 4163264 | 7206 | 1455674400 | 26697 | | 4163270 | 7205 | 1455685200 | 37497 | +---------+--------------------+------------+-------+ +---------+--------------------+------------+-------+ |id | grp | U id | utimes |值| +---------+--------------------+------------+-------+ | 4156187 | 5282 | 1455663600 | 15897 | | 4159888 | 5282 | 1455630000 | 26998 |* | 4156190 | 5282 | 1455676200 | 28497 | | 4156186 | 5282 | 1455661800 | 14097 | | 4156183 | 5282 | 1455652800 | 5097 | | 4156184 | 5282 | 1455656400 | 8697 | | 4156185 | 5282 | 1455660000 | 12297 | | 4156182 | 5282 | 1455651000 | 3297 |* | 4163311 | 7216 | 1455693000 | 45297 | | 4163275 | 7203 | 1455681600 | 33897 | | 4163309 | 7214 | 1455697800 | 50097 | | 4163308 | 7214 | 1455696000 | 48297 | | 4163307 | 7214 | 1455694200 | 46497 | | 4163306 | 7214 | 1455692400 | 44697 | | 4163305 | 7214 | 1455690600 | 42897 | | 4163304 | 7214 | 1455688800 | 41097 | | 4151121 | 4356 | 1455703200 | 55497 | | 4163271 | 7205 | 1455685500 | 37797 | | 4163272 | 7205 | 1455687000 | 39297 | | 4163269 | 7205 | 1455684900 | 37197 | | 4163273 | 7205 | 1455687300 | 39597 | | 4163264 | 7206 | 1455674400 | 26697 | | 4163270 | 7205 | 1455685200 | 37497 | +---------+--------------------+------------+-------+ 例子: unix时间戳:1455647703 +---------+--------------------+------------+-------+ |id | grp | U id | utimes |值| +---------+--------------------+------------+-------+ | 4159888 | 5282 | 1455630000 | 26998 | | 4156190 | 5282 | 1455651000 | 28497 | |4159889 | XYZ | 1455630000 | 26998| |4156191 | XYZ | 1455651000 | 28497| |4159883 | ABC | 1455630000 | 26998| |4156195 | ABC | 1455651000 | 28497| +---------+--------------------+------------+-------+
谢谢大家!

您可以通过
左连接操作来完成。
ON
子句包含“业务逻辑”:

SELECT t1.*
FROM mytable AS t1
LEFT JOIN mytable AS t2 
   ON t1.grp_id = t2.grp_id 
      AND 
     ((t1.utimes < 1455647703 AND t2.utimes < 1455647703 AND t2.utimes > t1.utimes)
       OR
      (t1.utimes > 1455647703 AND t2.utimes > 1455647703 AND t2.utimes < t1.utimes))
WHERE t1.grp_id = 5282 AND t2.id IS NULL
选择t1*
从mytable中选择t1
将mytable作为t2左键联接
在t1.grp\u id=t2.grp\u id上
及
((t1.utimes<1455647703和t2.utimes<1455647703和t2.utimes>t1.utimes)
或
(t1.utimes>1455647703和t2.utimes>1455647703和t2.utimes

请提供预期输出。你自己已经取得了什么成就?我认为正确的输出是标有星号的输出。这意味着最后结果集的第二条记录不正确。您说“当前unix时间戳的前一行和后一行”是什么意思?是否要为每个“grp_id”设置具有最小和最大“utimes”的行?否@MaxU,对于第一行,在一次内为每个grp_id找到“utimes current_timestamp”。感谢您的回答,使用grp_id=5282可以很好地工作。。但我可以将此应用于所有不同的grp_id吗?@c0rnw19是的,您可以。只需从
WHERE
子句中省略
t1.grp\u id=5282
。好的,非常感谢。但我有大约10万排,这需要很长时间( +---------+--------------------+------------+-------+ | id | grp_id | utimes | value | +---------+--------------------+------------+-------+ | 4159888 | 5282 | 1455630000 | 26998 | | 4156190 | 5282 | 1455651000 | 28497 | | 4159889 | XYZ | 1455630000 | 26998 | | 4156191 | XYZ | 1455651000 | 28497 | | 4159883 | ABC | 1455630000 | 26998 | | 4156195 | ABC | 1455651000 | 28497 | +---------+--------------------+------------+-------+
SELECT t1.*
FROM mytable AS t1
LEFT JOIN mytable AS t2 
   ON t1.grp_id = t2.grp_id 
      AND 
     ((t1.utimes < 1455647703 AND t2.utimes < 1455647703 AND t2.utimes > t1.utimes)
       OR
      (t1.utimes > 1455647703 AND t2.utimes > 1455647703 AND t2.utimes < t1.utimes))
WHERE t1.grp_id = 5282 AND t2.id IS NULL