tbl_cliente(id_cliente(pk),status,nome, etc...)
tbl_endereco(id_endereco(pk),status,cliente_id(fk),logradouro, etc...)
tbl_email(id_email(pk),status,cliente_email_id(fk),email, etc...)
tbl_telefone(id_telefone(pk),status,cliente_fone_id(fk), telefone, etc...)
and the relationship is 1 for many: a client has / can have multiple emails, phones, addresses
a query:
SELECT
C1.id_cliente,
C1.nome,
C1.fones,
C2.emails,
C2.endereco,
C2.numero
FROM
(SELECT
cliente.'status',
cliente.id_cliente,
cliente.nome,
GROUP_CONCAT(CONCAT_WS('|', telefone.telefone, telefone.tipo)) AS fones
FROM
cliente
LEFT JOIN telefone ON telefone.cliente_fone_id = cliente.id_cliente
WHERE
telefone.status = 1
GROUP BY cliente.id_cliente) AS C1
INNER JOIN
(SELECT
cliente.id_cliente,
endereco.endereco,
endereco.numero,
GROUP_CONCAT(email.email) AS emails
FROM
cliente
LEFT JOIN email ON email.cliente_email_id = cliente.id_cliente
LEFT JOIN endereco ON endereco.cliente_id = cliente.id_cliente
WHERE
email.status = 1 AND endereco.status = 1
GROUP BY cliente.id_cliente) C2 ON C1.id_cliente = C2.id_cliente
WHERE
C1.status = 1;
I need the query to fetch the remaining records even if the status of one or more of the conditionals is not satisfied. for example:
If the client does not have any email or if the emails are marked as inactive the query does not bring the other records from the other tables and that's what I'm trying to do. I'm using MariaDB.