簡體   English   中英

如何識別mysql中相同列中具有相同值的行? 而且我還必須向這些行添加條件

[英]How to identify rows that have the same value in same columns in mysql? and also i have to add condition to those rows

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 並且我必須找到時差

你可以使用exists

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

一種方法是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';

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM