2
respostas

Alter Table

Caros estou com o seguinte problema :

+----+-------------------+------------+------------------------+----------+------------+--------------+
| id | valor             | data       | observacoes            | recebido | forma_pgto | comprador_id |
+----+-------------------+------------+------------------------+----------+------------+--------------+
| 47 |               210 | 2008-02-19 | MATERIAL ESCOLAR       |        1 | boleto     |            2 |
| 48 |              3510 | 2008-05-21 | TELEVISAO              |        0 | boleto     |            2 |
| 49 |            1586.4 | 2008-04-30 | MATERIAL DE CONSTRUCAO |        1 | boleto     |            2 |
| 50 |            173.45 | 2008-12-15 | PIZZA PRA FAMILIA      |        1 | boleto     |            2 |
| 51 |              4790 | 2009-01-23 | SALA DE ESTAR          |        1 | boleto     |            2 |
| 52 |            402.15 | 2009-03-03 | QUARTOS                |        1 | boleto     |            2 |
| 53 |              1213 | 2009-03-18 | QUARTOS                |        1 | boleto     |            2 |
| 54 |             412.9 | 2009-03-21 | COPA                   |        1 | boleto     |            2 |
| 55 | 64.97999999999999 | 2009-04-12 | LANCHONETE             |        0 | boleto     |            2 |
| 57 |             78.65 | 2009-12-04 | entregue antes de 2011 |        1 | boleto     |            2 |
| 59 |             98.12 | 2009-07-09 | entregue antes de 2011 |        1 | boleto     |            2 |
| 60 |              2508 | 2009-01-12 | COMPRAS DE JANEIRO     |        1 | boleto     |            2 |
| 61 |            3212.4 | 2009-11-13 | entregue antes de 2011 |        1 | boleto     |            2 |
| 62 |            223.09 | 2009-12-17 | entregue antes de 2011 |        1 | boleto     |            2 |
| 63 |             778.9 | 2009-01-16 | FESTA                  |        1 | boleto     |            2 |
| 64 |             827.5 | 2010-01-09 | entregue antes de 2011 |        1 | boleto     |            2 |
| 66 |            678.43 | 2010-05-21 | entregue antes de 2011 |        1 | boleto     |            2 |
| 67 |          10937.12 | 2010-04-30 | entregue antes de 2011 |        1 | boleto     |            2 |
| 68 |              1501 | 2010-06-22 | entregue antes de 2011 |        1 | boleto     |            2 |
| 69 |              1709 | 2010-08-25 | PARCELA DA CASA        |        0 | boleto     |            2 |
| 70 |            567.09 | 2010-09-25 | PARCELA DO CARRO       |        0 | boleto     |            2 |
| 71 |            631.53 | 2010-10-12 | IPTU                   |        1 | boleto     |            2 |
| 72 |            909.11 | 2010-02-11 | entregue antes de 2011 |        1 | boleto     |            2 |
| 73 |            768.18 | 2010-04-10 | entregue antes de 2011 |        1 | boleto     |            2 |
| 74 |               434 | 2010-04-01 | entregue antes de 2011 |        1 | boleto     |            2 |
| 75 |             115.9 | 2010-06-12 | entregue antes de 2011 |        1 | boleto     |            2 |
| 76 |                98 | 2010-10-12 | DIA DAS CRIANÇAS      |        0 | boleto     |            2 |
| 77 |             253.7 | 2010-12-20 | NATAL - PRESENTES      |        0 | boleto     |            2 |
| 78 |            370.15 | 2010-12-25 | COMPRAS DE NATAL       |        0 | boleto     |            2 |
| 80 |            954.12 | 2011-11-03 | SHOW DA IVETE SANGALO  |        1 | boleto     |            2 |
| 81 |              98.7 | 2011-02-07 | LANCHONETE             |        1 | boleto     |            2 |
| 82 |             213.5 | 2011-09-25 | ROUPAS                 |        0 | boleto     |            2 |
| 83 |            1245.2 | 2011-10-17 | ROUPAS                 |        0 | boleto     |            2 |
| 85 |            576.12 | 2011-09-13 | SAPATOS                |        1 | boleto     |            2 |
| 87 |             87.43 | 2011-05-10 | GRAVATA                |        0 | boleto     |            2 |
| 88 |            887.66 | 2011-02-02 | PRESENTE PARA O FILHAO |        1 | boleto     |            2 |
| 90 |               500 | 2014-04-05 | presente para Mae      |        0 | boleto     |            2 |
| 91 |               500 | 2014-04-05 | presente para Mae 2    |        0 | boleto     |            2 |
| 92 |               500 | 2014-04-05 | presente da Mae 2      |        1 | cartao     |            2 |
+----+-------------------+------------+------------------------+----------+------------+--------------+
39 rows in set (0.00 sec)

Quero modificar a coluna comprador_id de forma a ela está relacionada com os recebidos. Qual seria a melhor forma de aplicar alter table de forma a sempre ser 1 quando for 0 recebimentos ?

2 respostas

Olá Guilherme,

Pelo que entendi, você quer alterar o comprador_id para 1 nas linhas onde o recebido é igual a 0, confere?

Isso pode ser resolvido usando o comando update com um condicional where, conforme o código abaixo:

UPDATE COMPRAS SET COMPRADOR_ID = 1 WHERE RECEBIDO = 0;

Olá Guilherme, conseguiu resolver o problema?

Se sim, por gentileza, marque o tópico como resolvido.

Obrigado.

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software