Try it this way (there are others):
SELECT ME.*
, CASE WHEN PA.idMembro IS NOT NULL
THEN 'pastores'
ELSE
( CASE WHEN RD.idLider IS NOT NULL
THEN 'redes'
ELSE
( CASE WHEN RE.idLider IS NOT NULL
THEN 'regioes'
ELSE
( CASE WHEN AR.idLider IS NOT NULL
THEN 'areas'
ELSE
( CASE WHEN SE.idLider IS NOT NULL
THEN 'setores'
ELSE
( CASE WHEN CE.idLider IS NOT NULL
THEN 'celulas'
END
)
END
)
END
)
END
)
END
)
END AS Tabela
FROM membros ME
LEFT JOIN pastores PA ON PA.idMembro = ME.idMembro
LEFT JOIN redes RD ON RD.idLider = ME.idMembro
LEFT JOIN regioes RE ON RE.idLider = ME.idMembro
LEFT JOIN areas AR ON AR.idLider = ME.idMembro
LEFT JOIN setores SE ON SE.idLider = ME.idMembro
LEFT JOIN celulas CE ON CE.idLider = ME.idMembro
WHERE PA.idMembro IS NOT NULL
OR RD.idLider IS NOT NULL
OR RE.idLider IS NOT NULL
OR AR.idLider IS NOT NULL
OR SE.idLider IS NOT NULL
OR CE.idLider IS NOT NULL
ORDER BY ME.nome
Optimizing (and completing) the query @Martinomadic, where the link to the table was missing membros
:
SELECT ME.*
, IFNULL(TMP.nomeTabela, '') AS Tabela
FROM membros ME
LEFT JOIN (
SELECT idMembro AS idMembro, 'pastores' AS nomeTabela FROM pastores
UNION
SELECT idLider AS idMembro, 'redes' AS nomeTabela FROM redes
UNION
SELECT idLider AS idMembro, 'regioes' AS nomeTabela FROM regioes
UNION
SELECT idLider AS idMembro, 'areas' AS nomeTabela FROM areas
UNION
SELECT idLider AS idMembro, 'setores' AS nomeTabela FROM setores
UNION
SELECT idLider AS idMembro, 'celulas' AS nomeTabela FROM celulas
) TMP ON TMP.idMembro = ME.idMembro
where you will be returning the name of the table where the id was found?
– Carlos Rocha
updated the consultation
– Marciano Machado
note, it’s Mysql ok. I put this query in Mysql Front and gave error
– Carlos Rocha
error here: :idMebroResult
– Carlos Rocha
Replace :idMebroSet with the id you want, or remove Where if you want to see all.
– Marciano Machado
table name is not a table field, but rather the same name, type pastors, networks... Where was found the record.
– Carlos Rocha
yes, the table name was a runtime field that creates along with joins in the subquerie
– Marciano Machado
is giving error here: :idMebroSee even when I change to :idMember
– Carlos Rocha
takes the 2 points in the answer ok? Where t.idMember = idMember
– Carlos Rocha
do not replace where idMember is by the id number you want to search for example: Where t.idMember = 1
– Marciano Machado
in php did so: Where t.idMember = " . $_idMember; where $_idMember is a function parameter. That’s right?
– Carlos Rocha