如何识别mysql中相同列中具有相同值的行?而且我还必须向这些行添加条件

问题描述 投票:0回答:2
id  bid   status   time
1   c1   close   2019.10.11
2   c2   close   2019.12.12
3   c2   open    2019.12.11
4   c3   close   2019.12.14

在这里我想同时捕获c2和我必须找到时差

mysql sql database mysql-workbench procedure
2个回答
2
投票

您可以使用exists

select t.*
from t
where exists (select 1 from t t1 where t1.bid = t.bid and t1.time <> t.time);

0
投票

一种方法是join

select to.*, tc.time as close_time,
       datediff(tc.time, to.time)
from t to join
     t tc
     on to.bid = tc.bid and
        to.status = 'open' and
        tc.status = 'close';
© www.soinside.com 2019 - 2024. All rights reserved.