如何识别 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';
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';