简体   繁体   中英

Select rows with same values and time interval MySQL

I have a big table in a DB in MySQL. This is only a limit 15 extract.

| name                 | etiqueta10      | msgtimestamp            | unixtime   |
+----------------------+-----------------+-------------------------+------------+
| SERTAN BOUEE         | 8a184cc44697fff | 2019-12-11 10:59:32.214 | 1576058372 |
| LONGLINE BUOY 06 89% | 8a3f664f6a47fff | 2019-12-11 02:29:54.387 | 1576027794 |
| LONGLINE BUOY 06 89% | 8a3f6641a08ffff | 2019-12-11 10:34:38.622 | 1576056878 |
| LONGLINE BUOY 06 89% | 8a3f664f6b07fff | 2019-12-11 03:26:43.41  | 1576031203 |
| LONGLINE BUOY 06 89% | 8a3f664f0c17fff | 2019-12-11 01:42:49.941 | 1576024969 |
| LONGLINE BUOY 06 89% | 8a3f664f6a77fff | 2019-12-11 02:35:26.123 | 1576028126 |
| LONGLINE BUOY 06 89% | 8a3f664f6b0ffff | 2019-12-11 03:10:00.187 | 1576030200 |
| LONGLINE BUOY 06 89% | 8a3f664f6a5ffff | 2019-12-11 05:50:06.411 | 1576039806 |
| LONGLINE BUOY 06 89% | 8a3f664f6a5ffff | 2019-12-11 04:18:54.378 | 1576027134 |
| LONGLINE BUOY 06 89% | 8a3f664f6a5ffff | 2019-12-11 06:22:39.546 | 1576041759 |
| LONGLINE BUOY 06 89% | 8a3f664f44a7fff | 2019-12-11 04:13:01.891 | 1576033981 |
| LONGLINE BUOY 06 89% | 8a3f6641a08ffff | 2019-12-11 10:20:59.305 | 1576056059 |
| LONGLINE BUOY 06 89% | 8a3f664f6a27fff | 2019-12-11 03:05:14.675 | 1576029914 |
| LONGLINE BUOY 06 89% | 8a3f664f0daffff | 2019-12-11 01:54:01.929 | 1576025641 |
| LONGLINE BUOY 06 89% | 8a3f664f4c9ffff | 2019-12-11 05:14:46.724 | 1576037686 |
+----------------------+-----------------+-------------------------+------------+

What I want is to select the rows where etiqueta10 have the same value and also the diffence between their unixtime values is lower than 7200. Unixtime is the conversion of the timestamp values.

I dont know if there is some mysql query to do that. I am very new working with DB systems. I have also thought doing some C code to do that.

Like in this extract I have three rows with same etiqueta10 value and with a difference lower than two hours of time. The result should be:

| LONGLINE BUOY 06 89% | 8a3f664f6a5ffff | 2019-12-11 05:50:06.411 | 1576039806 |
| LONGLINE BUOY 06 89% | 8a3f664f6a5ffff | 2019-12-11 04:18:54.378 | 1576027134 |
| LONGLINE BUOY 06 89% | 8a3f664f6a5ffff | 2019-12-11 06:22:39.546 | 1576041759 |

Thank you.

you can self join the table on etiqueta10 with the condition of unix time stamp difference < 7200

select * from <table_name> where etiqueta10 in (    
select a.etiqueta10 
from <table_name> a inner join <table_name> b
on a.etiqueta10=b.etiqueta10
where abs(a.unixtime-b.unixtime) between 0 and 7200);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM