使用更新

时间:2015-07-23 19:56:49

标签: mysql sql database subquery

我有以下查询:

SELECT 
        (SELECT 
            MIN(IFNULL(lead2.ID_FATHER,lead2.ID)) as ID_FATHER
        FROM 
            leads lead2
        WHERE
            lead2.TELEFONE_RESIDENCIAL = lead1.TELEFONE_RESIDENCIAL
            OR lead2.TELEFONE_RESIDENCIAL = lead1.TELEFONE_CELULAR
            OR lead2.TELEFONE_RESIDENCIAL = lead1.TELEFONE_COMERCIAL
            OR lead2.TELEFONE_CELULAR = lead1.TELEFONE_RESIDENCIAL
            OR lead2.TELEFONE_CELULAR = lead1.TELEFONE_CELULAR
            OR lead2.TELEFONE_CELULAR = lead1.TELEFONE_COMERCIAL
            OR lead2.TELEFONE_COMERCIAL = lead1.TELEFONE_RESIDENCIAL
            OR lead2.TELEFONE_COMERCIAL = lead1.TELEFONE_CELULAR
            OR lead2.TELEFONE_COMERCIAL = lead1.TELEFONE_COMERCIAL
            OR lead2.EMAIL = lead1.EMAIL),
        lead1.ID
FROM 
    leads lead1
LIMIT 1000

我需要做到这一点,我是一个更新。 我试过但失败了。

UPDATE 
    leads as lead1
SET
    ID_FATHER= (SELECT 
            MIN(IFNULL(lead2.ID_FATHER,lead2.ID)) 
        FROM 
            leads lead2
        WHERE
            lead2.TELEFONE_RESIDENCIAL = lead1.TELEFONE_RESIDENCIAL
            OR lead2.TELEFONE_RESIDENCIAL = lead1.TELEFONE_CELULAR
            OR lead2.TELEFONE_RESIDENCIAL = lead1.TELEFONE_COMERCIAL
            OR lead2.TELEFONE_CELULAR = lead1.TELEFONE_RESIDENCIAL
            OR lead2.TELEFONE_CELULAR = lead1.TELEFONE_CELULAR
            OR lead2.TELEFONE_CELULAR = lead1.TELEFONE_COMERCIAL
            OR lead2.TELEFONE_COMERCIAL = lead1.TELEFONE_RESIDENCIAL
            OR lead2.TELEFONE_COMERCIAL = lead1.TELEFONE_CELULAR
            OR lead2.TELEFONE_COMERCIAL = lead1.TELEFONE_COMERCIAL
            OR lead2.EMAIL = lead1.EMAIL)
LIMIT 1000

当我尝试运行代码时,错误,而不是更新。

任何建议,我都尝试过其他方法,但到目前为止还没有。

_______ 2015年7月24日更新_____________ _____________

UPDATE 
    leads as lead,
    (SELECT
            (SELECT 
                MIN(IFNULL(lead2.ID_PAI,lead2.ID)) as ID_FATHER
            FROM 
                leads lead2,
                empresas empr2
            WHERE
                empr2.FK_GRUPOS_ID = empr1.FK_GRUPOS_ID
                AND(lead2.TELEFONE_RESIDENCIAL = lead1.TELEFONE_RESIDENCIAL
                OR lead2.TELEFONE_RESIDENCIAL = lead1.TELEFONE_CELULAR
                OR lead2.TELEFONE_RESIDENCIAL = lead1.TELEFONE_COMERCIAL
                OR lead2.TELEFONE_CELULAR = lead1.TELEFONE_RESIDENCIAL
                OR lead2.TELEFONE_CELULAR = lead1.TELEFONE_CELULAR
                OR lead2.TELEFONE_CELULAR = lead1.TELEFONE_COMERCIAL
                OR lead2.TELEFONE_COMERCIAL = lead1.TELEFONE_RESIDENCIAL
                OR lead2.TELEFONE_COMERCIAL = lead1.TELEFONE_CELULAR
                OR lead2.TELEFONE_COMERCIAL = lead1.TELEFONE_COMERCIAL
                OR lead2.EMAIL = lead1.EMAIL)) ID_FATHER,
            lead1.ID
    FROM 
        leads lead1,
        empresas empr1
    WHERE 
        lead1.FK_EMPRESAS_ID = empr1.ID
    )AS TD
SET lead.ID_PAI = IFNULL(TD.ID_FATHER,TD.ID)
WHERE lead.ID = TD.ID

1 个答案:

答案 0 :(得分:1)

试试这个:

UPDATE lead1
SET
lead1.ID_FATHER= MIN(IFNULL(lead2.ID_FATHER,lead2.ID)) 

    FROM leads as lead1,
        leads as lead2
    WHERE
        lead2.TELEFONE_RESIDENCIAL = lead1.TELEFONE_RESIDENCIAL
        OR lead2.TELEFONE_RESIDENCIAL = lead1.TELEFONE_CELULAR
        OR lead2.TELEFONE_RESIDENCIAL = lead1.TELEFONE_COMERCIAL
        OR lead2.TELEFONE_CELULAR = lead1.TELEFONE_RESIDENCIAL
        OR lead2.TELEFONE_CELULAR = lead1.TELEFONE_CELULAR
        OR lead2.TELEFONE_CELULAR = lead1.TELEFONE_COMERCIAL
        OR lead2.TELEFONE_COMERCIAL = lead1.TELEFONE_RESIDENCIAL
        OR lead2.TELEFONE_COMERCIAL = lead1.TELEFONE_CELULAR
        OR lead2.TELEFONE_COMERCIAL = lead1.TELEFONE_COMERCIAL
        OR lead2.EMAIL = lead1.EMAIL)