2
Two tables tbl_orcamento
and tbl_itens
the budget table has a column with the following status.
1 = orcamento aprovado
2 = ordem de compra emitida
3 = ordem de compra aprovada
To tbl_itens
has some columns I will list the ones that will be used in the query.
- id_tbl_budget
- date of receipt
- n_nf_received
and query I am trying to make is to know if all the items of the request have arrived, the way I am using 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 believe that in the second SELECT the instruction is not understanding the id
id_tbl_budget = tbl_budget, there is a way to pass the variable from the first instruction to the second.
This is the SELECT
internal corresponding to the WHERE
leading
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
Which must check if the items referring to the request have number of nf, and if it is equal to zero still this pending then the request is incomplete, if all are with number of nf the value will be 100 and so satisfy the query.
My doubt lies in this line of QUERY
id_tbl_budget = tbl_budget.id
'Cause I’m not passing a figure, I’m trying to take it dynamically.
Thus the QUERY
works.
id_tbl_budget = '123'
Do you get any errors? I don’t understand the problem
– Roberto de Campos
I receive NULL, but if I do the internal query stating a valid id I receive the percentage value of items received and if everything has been received I receive 100.0000
– WMomesso
But you get
NULL
in which query? can further specify your problem in the question?– Roberto de Campos
I receive null in the question statement. I am suspicious that the second select is not receiving the id number of the table tbl_orcing.
– WMomesso
I edited the question I hope to understand
– WMomesso
Try to do it this way:
SELECT * FROM tbl_orcamento a
INNER JOIN tbl_itens b ON a.id = b.id_tbl_orcamento
WHERE (
 SELECT SUM(IF(a1.n_nf_recebida != 0, 1, 0)) / COUNT(a1.id) * 100
 FROM tbl_itens a1
 WHERE a1.id_tbl_orcamento = a.id
) = 100;
– Roberto de Campos