Dichiarazione di aggiornamento MYSQL tabelle join interno


201

Non ho idea di quale sia il problema. Usando MySQL 5.0 ottengo un errore di compilazione quando tento di eseguire la seguente dichiarazione di aggiornamento MYSQL

  UPDATE  b
SET b.mapx = g.latitude,
  b.mapy = g.longitude
FROM business AS b
INNER JOIN business_geocode g ON b.business_id = g.business_id
WHERE  (b.mapx = '' or b.mapx = 0) and
  g.latitude > 0

tutti i nomi dei campi sono corretti. qualche idea?


ho anche rimosso l'alias, quando lo cambio in un SELECT b. * DA business b inner join funziona
Vibration Of Life

Risposte:


433

Prova questo:

UPDATE business AS b
INNER JOIN business_geocode AS g ON b.business_id = g.business_id
SET b.mapx = g.latitude,
  b.mapy = g.longitude
WHERE  (b.mapx = '' or b.mapx = 0) and
  g.latitude > 0

Aggiornare:

Dal momento che hai detto che la query ha prodotto un errore di sintassi, ho creato alcune tabelle su cui ho potuto testarlo e ho confermato che non ci sono errori di sintassi nella mia query:

mysql> create table business (business_id int unsigned primary key auto_increment, mapx varchar(255), mapy varchar(255)) engine=innodb;
Query OK, 0 rows affected (0.01 sec)

mysql> create table business_geocode (business_geocode_id int unsigned primary key auto_increment, business_id int unsigned not null, latitude varchar(255) not null, longitude varchar(255) not null, foreign key (business_id) references business(business_id)) engine=innodb;
Query OK, 0 rows affected (0.01 sec)

mysql> UPDATE business AS b
    -> INNER JOIN business_geocode AS g ON b.business_id = g.business_id
    -> SET b.mapx = g.latitude,
    ->   b.mapy = g.longitude
    -> WHERE  (b.mapx = '' or b.mapx = 0) and
    ->   g.latitude > 0;
Query OK, 0 rows affected (0.00 sec)
Rows matched: 0  Changed: 0  Warnings: 0

Vedere? Nessun errore di sintassi. Ho provato con MySQL 5.5.8.


L'ho provato e sto ottenendo lo stesso errore. - Errore durante l'ottenimento del piano di esecuzione: si è verificato un errore nella sintassi SQL; controlla il manuale corrispondente alla versione del tuo server MySQL per la sintassi corretta da utilizzare vicino a "AGGIORNA business come b INNER JOIN business_geocode g ON b.business_id = g.busines" alla riga 1
Vibration Of Life

Si prega di pubblicare i risultati di show create table business;e show create table business_geocode;così posso testare un po 'meglio la mia query. Grazie.
Asaph,

Non c'è errore di sintassi. Ho appena confermato e aggiornato la mia risposta.
Asaph,

4
@ user719316: C'è qualcosa di sospetto prima di quella query ... ti manca un punto e virgola?
Bobby,

2
@Joakim La ASparola chiave è facoltativa. Ma da quando l'hai menzionato, l'ho aggiunto alla risposta per motivi di coerenza, dal momento che l'ho usato sul primo alias nella stessa query.
Asaph,

15

La SETclausola dovrebbe venire dopo le specifiche della tabella.

UPDATE business AS b
INNER JOIN business_geocode g ON b.business_id = g.business_id
SET b.mapx = g.latitude,
  b.mapy = g.longitude
WHERE  (b.mapx = '' or b.mapx = 0) and
  g.latitude > 0

-2

Per MySql WorkBench, utilizzare di seguito:

update emp as a
inner join department b on a.department_id=b.id
set a.department_name=b.name
where a.emp_id in (10,11,12); 
Utilizzando il nostro sito, riconosci di aver letto e compreso le nostre Informativa sui cookie e Informativa sulla privacy.
Licensed under cc by-sa 3.0 with attribution required.