Mysql
 sql >> Database >  >> RDS >> Mysql

SQL self-join per restituire righe specifiche

SELECT  mm.*, md.label AS standardized_label
FROM    mytable md
LEFT JOIN
        mytable mc
ON      mc.parent_element = md.id
        AND mc.client = @client
JOIN    mytable mm
ON      mm.id = COALESCE(mc.id, md.id)
WHERE   md.client IS NULL

Crea un indice su (client, parent_element) affinché funzioni velocemente.

Vedi SQLFiddle .