我正在做一个查询,从标题为ID,IDDriver,StatusActivation,DateCreation的名为“ tvp”的BBDD中提取数据
当我想用“ StatusActivation” 0和下一条记录“ StatusActivation” 1减去“ DateCreation”以获取连接时间时,在某些行的“ DateCreation”中重复“ StatusActivation”(0或1)
关于图片(链接)的示例,对不起朋友,我不得不上传带有链接的图片,因为从未创建过txt中的表格。
https://ibb.co/r54PsBt
这是针对MySQL Workbench 8.0 CE
在这里,一个小提琴:DB Fiddle
SELECT
ID
,IDDriver
,StatusActivation
,DateCreation
FROM
tb_logs
WHERE
DateCreation between '2019-01-01 00:00:00' and '2019-01-31 00:00:00';
我希望输出是“ StatusActivation” 0或1,不要在“ DateCreation”和“ IDDriver”序列中重复
最佳答案
您可以使用如下所示的内容:
DELETE tb_logs WHERE StatusActivation=0 and ID Not in (SELECT MIN(ID) FROM tbl_logs
WHERE StatusActivation=0 group by IDDriver )
DELETE tb_logs WHERE StatusActivation=1 and ID Not in (SELECT MAX(ID) FROM tbl_logs
WHERE StatusActivation=1 group by IDDriver )