SELECT COMO CLAUSULA NO WHERE

04/02/2019

0

O select me mostra o valor da coluna restcota do penultimo registro. E dependendo se esse valor for maior que zero, irei atualizar o ultimo registro. Estou usando esse select como clausula.

Este é o Select que me mostra o valor da coluna do registro anterior, está executando certinho:

SELECT restcota FROM tbxerox x
order by xerox desc limit 1,1;



E este é o UPDATE, mas está emitindo erro : Error Code: 1093. You can't specify target table 'x' for update in FROM clause

UPDATE tbxerox x
INNER JOIN tbservidores s on s.cpfserv = x.cpf
set x.valor = (s.maxcot - s.total_xerox) * (-0.10)
where
(SELECT x.restcota
FROM tbxerox x
where x.cpf = x.cpf
order by x.xerox desc limit 1,1) > 0

and (x.restcota) < 0
and (s.maxcot - x.qtd_xerox) < 0
and (x.qtd_xerox) > 0
and (s.total_xerox) > 80
and x.xerox = ultimo_Xerox ()
and s.tiposerv = 'Professor';
Igor

Igor

Responder

Post mais votado

05/02/2019

Tente utilizar outro nome no select, que parece que ele tá se perdendo nesse x em 2 tabelas distintas.

UPDATE
tbxerox x INNER JOIN tbservidores s on s.cpfserv = x.cpf
set
x.valor = ((s.maxcot - s.total_xerox) * (-0.10))
where
( SELECT x1.restcota
FROM tbxerox x1
where x1.cpf = x1.cpf
order by x1.xerox desc limit 1,1) > 0
and (x.restcota) < 0
and (s.maxcot - x.qtd_xerox) < 0
and (x.qtd_xerox) > 0
and (s.total_xerox) > 80
and x.xerox = ultimo_Xerox ()
and s.tiposerv = 'Professor';

Kleber Santos

Kleber Santos
Responder

Mais Posts

05/02/2019

Igor

Tente utilizar outro nome no select, que parece que ele tá se perdendo nesse x em 2 tabelas distintas.

UPDATE
tbxerox x INNER JOIN tbservidores s on s.cpfserv = x.cpf
set
x.valor = ((s.maxcot - s.total_xerox) * (-0.10))
where
( SELECT x1.restcota
FROM tbxerox x1
where x1.cpf = x1.cpf
order by x1.xerox desc limit 1,1) > 0
and (x.restcota) < 0
and (s.maxcot - x.qtd_xerox) < 0
and (x.qtd_xerox) > 0
and (s.total_xerox) > 80
and x.xerox = ultimo_Xerox ()
and s.tiposerv = 'Professor';


Emitiu o mesmo erro
Responder

06/02/2019

Jorge Clésio

Bom. Pode ser inconsistência nesse Join!<br />
<br />
Tente utilizar outro nome no select, que parece que ele tá se perdendo nesse x em 2 tabelas distintas.<br />
<br />
UPDATE <br />
tbxerox x INNER JOIN tbservidores s on s.cpfserv = x.cpf<br />
set <br />
x.valor = ((s.maxcot - s.total_xerox) * (-0.10))<br />
where<br />
( SELECT x1.restcota<br />
FROM tbxerox x1<br />
where x1.cpf = x1.cpf<br />
order by x1.xerox desc limit 1,1) > 0<br />
and (x.restcota) < 0<br />
and (s.maxcot - x.qtd_xerox) < 0<br />
and (x.qtd_xerox) > 0<br />
and (s.total_xerox) > 80<br />
and x.xerox = ultimo_Xerox ()<br />
and s.tiposerv = ''Professor'';
Responder

06/02/2019

Kleber Santos

Essa parte não está na sintaxe SQL: "and x.xerox = ultimo_Xerox ()", parece ser uma função da aplicação.



Se certifique se essas colunas são do tipo numero:
"x.valor = ((s.maxcot - s.total_xerox) * (-0.10))"



Faça alguns testes, excluindo linha por linha do update até ele não apresentar mais o erro.

Poste as tabelas para a gente analisar.


Responder

06/02/2019

Kleber Santos

Analisando melhor,
Esse SELECT está errado, não precisa do "where x.cpf = x.cpf"
E também não precisa do SELECT, pelo que está no código você precisa que o x.restcota seja diferente de 0

antes:
...( SELECT x1.restcota
FROM tbxerox x1
where x1.cpf = x1.cpf
order by x1.xerox desc limit 1,1) > 0
and (x.restcota) < 0...

depois:
...(x.restcota) <> 0...

Use assim:

UPDATE
tbxerox x INNER JOIN tbservidores s on s.cpfserv = x.cpf
set
x.valor = ((s.maxcot - s.total_xerox) * (-0.10))
where
(x.restcota) <> 0
and (s.maxcot - x.qtd_xerox) < 0
and (x.qtd_xerox) > 0
and (s.total_xerox) > 80
and x.xerox = ultimo_Xerox ()
and s.tiposerv = 'Professor';
Responder

06/02/2019

Igor

Analisando melhor,
Esse SELECT está errado, não precisa do "where x.cpf = x.cpf"
E também não precisa do SELECT, pelo que está no código você precisa que o x.restcota seja diferente de 0

antes:
...( SELECT x1.restcota
FROM tbxerox x1
where x1.cpf = x1.cpf
order by x1.xerox desc limit 1,1) > 0
and (x.restcota) < 0...

depois:
...(x.restcota) <> 0...

Use assim:

UPDATE
tbxerox x INNER JOIN tbservidores s on s.cpfserv = x.cpf
set
x.valor = ((s.maxcot - s.total_xerox) * (-0.10))
where
(x.restcota) <> 0
and (s.maxcot - x.qtd_xerox) < 0
and (x.qtd_xerox) > 0
and (s.total_xerox) > 80
and x.xerox = ultimo_Xerox ()
and s.tiposerv = 'Professor';






Este é o Select que me mostra o valor da coluna do registro anterior. Preciso usar ele como critério pra atualizar o ultimo registro. Entendeu?

SELECT restcota FROM tbxerox x
order by xerox desc limit 1,1;
Responder

Assista grátis a nossa aula inaugural

Assitir aula

Saiba por que programar é uma questão de
sobrevivência e como aprender sem riscos

Assistir agora

Utilizamos cookies para fornecer uma melhor experiência para nossos usuários, consulte nossa política de privacidade.

Aceitar