I have 3 left joined tables in MySql.
contratos
, which stores customer data
funcionario
, which stores employee data
cobranca
, which stores every customer charge.
I want to generate a report based on charge status
. But each customer charge has many status
, and i want to retrieve the current status
.
The following code returns the last update from cobrancas
table.
SELECT cob.status, cob.created, con.data_venda, cpn.contrato, con.razao_social, con.cnpj, con.valor, f.nick
FROM cobrancas cob
LEFT JOIN contratos con
ON c.id = cob.contrato
LEFT JOIN funcionarios f
ON f.id = cob.cobrador
WHERE 1=1
ORDER BY cob.created DESC
LIMIT 1
But it returns without a status
filter. If i put a WHERE clause like cob.status = 'x'
, it returns the last record with this status
, but it may not be the current. So how can i check if cob.status
is the current status
in WHERE clause to decide if i will put it in the list? Something like:
WHERE IF(cob.status == the last status inserted AND cob.status == 'x')
Can you understand what i want to do? Thank you.
One solution to use subquery
select * from
(
SELECT cob.status, cob.created, con.data_venda, cpn.contrato, con.razao_social, con.cnpj, con.valor, f.nick
FROM cobrancas cob
LEFT JOIN contratos con
ON c.id = cob.contrato
LEFT JOIN funcionarios f
ON f.id = cob.cobrador
WHERE 1=1
ORDER BY cob.created DESC
LIMIT 1
) where status = 'X'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.