Two tables tbl_orcamento
and tbl_itens
the table budgets have a column with the following statuses.
1 = orcamento aprovado
2 = ordem de compra emitida
3 = ordem de compra aprovada
The tbl_itens
has some columns I will list the ones that will be used in the query.
- id_tbl_orcamento
- data_received
- n_nf_received
and the query I am trying to do is to know if all of the order items have arrived, the way I am using it is below.
SELECT * FROM tbl_orcamento
INNER JOIN tbl_itens ON tbl_orcamento.id = tbl_itens.id_tbl_orcamento
WHERE (SELECT COUNT(q.id) / t.total * 100 FROM tbl_itens q,
(SELECT COUNT(*) as total FROM tbl_itens
WHERE id_tbl_orcamento = tbl_orcamento.id ) t
WHERE id_tbl_orcamento = tbl_orcamento.id AND n_nf_recebida != 0) = 100.0000
I think that in the second SELECT the statement is not understanding id
id_tbl_orcamento = tbl_orcamento_id , there is a way to pass the variable from the first statement to the second.
This is the% internal% of the corresponding SELECT
main
SELECT COUNT(q.id) / t.total * 100 FROM tbl_itens q,
(SELECT COUNT(*) as total FROM tbl_itens
WHERE id_tbl_orcamento = tbl_orcamento.id ) t
WHERE id_tbl_orcamento = tbl_orcamento.id AND n_nf_recebida != 0
You should check if the items referring to the request have nf number, and if it is equal to zero is still pending then the request is incomplete, if all are nf the value will be 100 and thus satisfy the query
My question is in this line of WHERE
id_tbl_origin = tbl_orcamento.id
Because I'm not passing a value, I'm trying to catch it dynamically.
That way QUERY
works.
id_tbl_orcamento = '123'