Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 对于这个复杂的场景,在SQL中使用递归CTE可以做到这一点吗?请告诉我其他的方法_Mysql_Sql_Recursive Query - Fatal编程技术网

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;