How to go through the tuples for an ID and check their values?

Asked

Viewed 211 times

1

In a table where I record the amount of plots and the situation of each of them, I need to go through it and find the sales orders where all plots are paid.

Ex:

SELECT * FROM tabela_parcelas WHERE ID = X

Returns:

ID       Parcela | Situacao  
123456 |  1      | Pago  
123456 |  2      | Pendente 
123456 |  3      | Pendente
000002 |  1      | Pago
000002 |  2      | Pago

@Edit I need to check in the "tabela_parcelas" if all parcels of the XXX order have been paid, so that in the "orders tabela_pedidos" I can change the status of the purchase to "complete" or keep "pending".

The possible status of each plot is:

// 00 - pagamento efetuado 
// 01 - pagamento não autorizado/transação recusada 
// 02 - erro no processamento da consulta 
// 03 - pagamento não localizado 
// 10 - campo “idConv” inválido ou nulo 
// 11 - valor informado é inválido, nulo ou não confere com o valor registrado 
// 21 - Pagamento Web não autorizado 
// 22 - erro no processamento da consulta 
// 23 - erro no processamento da consulta
// 24 - Convênio não cadastrado 
// 25 - Convênio não ativo 
// 26 - Convênio não permite debito em conta 
// 27 - Serviço inválido 
// 28 - Boleto emitido 
// 29 - pagamento não efetuado
// 30 - erro no processamento da consulta 
// 99 - Operação cancelada pelo cliente 
  • Marcelo the situation field is numerical then?

  • Yes, @gmsantos.

3 answers

3

If ID is numeric

SELECT * FROM tabela_parcelas WHERE ID = 2 AND Situacao="Pago"

if it’s string

SELECT * FROM tabela_parcelas WHERE ID = "000002" AND Situacao="Pago"


Edit: After your comment and trying to guess what you need, maybe it’s something along those lines (just example, of course. the syntax is not that):

SELECT * FROM tabela_ordens WHERE (SELECT tabela_parcelas (NAO-RETORNA-NADA-PENDENTE") )

I await your Dit on the question to improve the answer (or for other participants to answer, of course).

  • Bacchus, my example was ridiculously simple. I will do this in a table and need to bring the records without referencing a specific ID as we did in the examples. Something like: SELECT * FROM tabela_parcels WHERE ( checks all lines of id x and.

  • 2

    @Marcelodeandrade try to edit the question and make it clearer (explain in a very objective way what you want, preferably with the specific case, because the comment was the same). I am leaving, but on the way back I can try to edit the answer, or until then someone can give the appropriate answer to what you want (as long as you are objective, and explain what you want clearly, obviously). The "edit" button is just below the question. Try to be detailed for a better chance of getting an answer that suits you.

  • Edited, tried to simplify.

0


Resolved as follows:

SELECT * FROM `tabela_parcelas ` A1 WHERE NOT EXISTS ( SELECT * from `tabela_parcelas ` A2 where A1.id = A2.id and A2.situacao <> 0 ) ORDER BY id desc

0

Instead of getting everything that’s already been paid for, why not get everything that has nothing pending?

SELECT * FROM tabela_parcelas WHERE id NOT IN 
(SELECT id FROM tabela_parcelas WHERE situacao = 'Pendente');

Example in sqlfiddle

I am assuming that there are only two situations: Pago and Pendende. If there are others, no select interior you need to put the other situations using a or

As the posted status codes, as they are numeric as per your comment, simply change the query status field to greater than 0.

SELECT * FROM tabela_parcelas WHERE id NOT IN 
(SELECT id FROM tabela_parcelas WHERE situacao > 0);
  • There are other status. I need to check in the "tabela_parcelas" if all parcels of the XXX order have been paid, so that in the "orders tabela_pedidos" I can change the status of the purchase to "complete" or keep "pending".

  • @Rcelo edits your question including all possible status.

  • includes the list of codes used.

  • Even so, he still keeps records of installments with outstanding payments, @gmsantos. Example: When I searched the query, I got ID 2829 with installments 1 and 2 paid, but the 3rd is still pending. So he wasn’t supposed to be on the list, he was only supposed to come if all three installments were paid.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.