Mysql 对于这个复杂的场景,在SQL中使用递归CTE可以做到这一点吗?请告诉我其他的方法
我的场景:Mysql 对于这个复杂的场景,在SQL中使用递归CTE可以做到这一点吗?请告诉我其他的方法,mysql,sql,recursive-query,Mysql,Sql,Recursive Query,我的场景: select ID, Email, MobileNo, DeviceId from (select * from tableuser order by ID, Email) tableuser_sorted, (select @pv := '0122338737', @pc= 'DF1234') initialisation where find_in_set(MobileNo, @pv)
select ID,
Email,
MobileNo,
DeviceId
from (select * from tableuser
order by ID, Email) tableuser_sorted,
(select @pv := '0122338737', @pc= 'DF1234') initialisation
where find_in_set(MobileNo, @pv)
and length(@pv := concat(@pv, ',', ID))
OR find_in_set(DeviceId, @pc) and length(@pc := concat(@pc,',', ID)
- 用户A是(欺诈者)
- 用户B不是(欺诈者)。但是,系统将不允许用户B 做任何动作。因为B和A使用同一部电话 编号(与欺诈用户共享属性)。(1层)
- 用户D不是(欺诈者)。但是D与B使用相同的设备ID B是与欺诈用户共享属性。然后也阻止用户D。在这种情况下,有两层。D与B比较,B与A比较
select ID,
Email,
MobileNo,
DeviceId
from (select * from tableuser
order by ID, Email) tableuser_sorted,
(select @pv := '0122338737', @pc= 'DF1234') initialisation
where find_in_set(MobileNo, @pv)
and length(@pv := concat(@pv, ',', ID))
OR find_in_set(DeviceId, @pc) and length(@pc := concat(@pc,',', ID)
输出:
我要找的是:
select ID,
Email,
MobileNo,
DeviceId
from (select * from tableuser
order by ID, Email) tableuser_sorted,
(select @pv := '0122338737', @pc= 'DF1234') initialisation
where find_in_set(MobileNo, @pv)
and length(@pv := concat(@pv, ',', ID))
OR find_in_set(DeviceId, @pc) and length(@pc := concat(@pc,',', ID)
用户A是欺诈者。好的,与其他用户一起检查用户A属性。好的,我发现B正在与A共享电话号码。B将处于第二阶段。好,现在与其他用户一起检查用户B属性。好的,我发现D正在与B共享DeviceId,依此类推。您可以使用递归CTE来实现此目的。如果你只想要欺诈者,这样的方法应该有效:
with recursive cte as (
select ID, Email, MobileNo, DeviceId, id as ids
from tableuser
where isfraudsterstatus = 1
union all
select u.id, u.email, u.mobileno, u.deviceid, concat_ws(',', cte.ids, u.id)
from cte join
tableuser u
on u.email = cte.email or
u.mobileno = cte.mobileno or
u.deviceid = cte.deviceid
where find_in_set(u.id, cte.ids) = 0
)
select distinct id
from cte;