Ho una query UPDATE in cui faccio riferimento in modo esplicito al database, ma MySQL continua a lamentarsi del messaggio: ERROR 1046 (3D000): No database selected
.MySQL "ERROR 1046 (3D000): nessun database selezionato" nella query di aggiornamento
Altre query simili a struttura, ma utilizzano un valore di lavoro INSERT. Anche altre query che eseguono solo SELECT funzionano correttamente.
Per ripetere il problema in un banco di prova, provare a eseguire queste query:
create table test.object1 (
id_object1 int unsigned not null auto_increment,
total int,
weight int,
dt datetime,
primary key (id_object1)
) engine=InnoDB;
create table test.object2 (
id_object2 int unsigned not null auto_increment,
primary key (id_object2)
) engine=InnoDB;
create table test.score (
id_object1 int unsigned not null,
id_object2 int unsigned not null,
dt datetime,
score float,
primary key (id_object1, id_object2),
constraint fk_object1 foreign key (id_object1) references object1 (id_object1),
constraint fk_object2 foreign key (id_object2) references object2 (id_object2)
) engine=InnoDB;
insert into test.object1 (id_object1, total, weight, dt) values (1, 0, 0, '2012-01-01 00:00:00');
insert into test.object1 (id_object1, total, weight, dt) values (2, 0, 0, '2012-01-02 00:00:00');
insert into test.object2 (id_object2) values (1);
insert into test.score (id_object1, id_object2, dt, score) values (1, 1, '2012-01-03 00:00:00', 10);
insert into test.score (id_object1, id_object2, dt, score) values (2, 1, '2012-01-04 00:00:00', 8);
update test.object1 p
join (
select ur.id_object1, sum(ur.score * ur.weight) as total, count(*) as weight
from (
select lur.*
from (
select s.id_object1, s.id_object2, s.dt, s.score, 1 as weight
from test.score as s
join test.object1 as o1 using(id_object1)
where s.dt > o1.dt
order by s.id_object1, s.id_object2, s.dt desc
) as lur
group by lur.id_object2, lur.id_object1, date(lur.dt)
order by lur.id_object1, lur.id_object2
) as ur
group by ur.id_object1
) as r using(id_object1)
set
p.total = p.total + r.total,
p.weight = p.weight + r.weight,
p.dt = now();
Nota: Io corro queste query da un ambiente PHP e non ho usato esplicitamente mysql_select_db ('test'), perché preferisco non farlo e nessuna delle altre (molte!) richieste lo richiede. Sono sicuro che usare mysql_select_db risolverà il mio problema, ma vorrei sapere perché esattamente questa particolare query non funziona.
Per l'amor di confronto: se ci si esegue questa query più semplice, anche senza l'utilizzo di mysql_select_db, tutto funziona bene:
update test.object1 set total=1, weight=1, dt=now() where id_object1=1;
ho cercato inutilmente. L'unica cosa che ho trovato che è venuto vicino, è stato questo bug report: http://bugs.mysql.com/bug.php?id=28551 e soprattutto che ultimo messaggio (senza risposta) ...
Probabilmente è solo il primo ... – Shoe
questo potrebbe sembrare sciocco, ma potresti sostituire "now()" con una data manuale casuale invece e riprovare? – Sebas
@Sebas Ho appena fatto, nessuna differenza. Perché dovrebbe importare? – webtweakers