Java com MySQL

3 respostas
P

Pessoal, estou implmentando um método em Java e tenho que atualizar um campo de um registro de acordo com o campo de outro registro da mesma tabela.

Estou fazendo assim:
UPDATE TMP_iRIS SET GRUPO = (SELECT GRUPO FROM TMP_IRIS WHERE ID = 1) WHERE ID = 2;

Mas está dando o seguinte erro:

You can’t specify target table ‘tmp_iris’ for update in FROM clause

Se alguém puder me ajudar, agradeço muito.

3 Respostas

rafaelglauber

A subquery can return a scalar (a single value), a single row, a single column, or a table (one or more rows of one or more columns). These are called scalar, column, row, and table subqueries. Subqueries that return a particular kind of result often can be used only in certain contexts, as described in the following sections.

There are few restrictions on the type of statements in which subqueries can be used. A subquery can contain any of the keywords or clauses that an ordinary SELECT can contain: DISTINCT, GROUP BY, ORDER BY, LIMIT, joins, index hints, UNION constructs, comments, functions, and so on.

One restriction is that a subquery’s outer statement must be one of: SELECT, INSERT, UPDATE, DELETE, SET, or DO. Another restriction is that currently you cannot modify a table and select from the same table in a subquery. This applies to statements such as DELETE, INSERT, REPLACE, UPDATE, and (because subqueries can be used in the SET clause) LOAD DATA INFILE.

A more comprehensive discussion of restrictions on subquery use, including performance issues for certain forms of subquery syntax, is given in Section F.3, ?Restrictions on Subqueries?.

retirado de: http://dev.mysql.com/doc/refman/5.0/en/subqueries.html

P

Consegui fazern da seguinte maneira e deu certo:

UPDATE TMP_IRIS AS T1, TMP_IRIS AS T2 SET T1.GRUPO = 1 WHERE T2.ID = 2;

rafaelglauber

velho, de boa, dê uma estudada em SQL, esse comanda não tem muito sentido mesmo tendo executado…não tem necessidade de ter a TMP_IRIS com mais de uma declaração, bastava: UPDATE TMP_IRIS AS T1 SET T1.GRUPO = 1 WHERE T1.ID = 2 você pode gerar problemas diversos se não dominar SQL, ainda mais em 1 UPDATE.

Criado 26 de outubro de 2007
Ultima resposta 26 de out. de 2007
Respostas 3
Participantes 2