0
Hello, I created an sql query to return all the client data in a single query, this query works partially if all conditions are satisfied, but this system uses soft delete, where all records cannot be deleted and only marked as inactive. the status field is marked with Zero (0) for inactive and One (1) for active. the tables are:
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 customer has/may have multiple emails, phones, addresses
the 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 search the other records even if the status of one or more of the conditional ones is not satisfied. for example:
If the customer has no email or if the emails are marked as inactive the query does not back the other records from the other tables and that’s what I’m trying to do. I’m wearing the Mariadb.
@gmsantos, grateful for the correction!
– FerraBraiZ
You want to select all active customers, regardless of whether they have active emails or not?
– gmsantos
I want to select all customer data, except those marked as inactive, ie status = 0.
– FerraBraiZ