MySQL "ERROR 1046 (3D000): No database selected" in the update request

I have the UPDATE query, where I explicitly refer to the database, but MySQL still complains about the message: ERROR 1046 (3D000): No database selected.

Other queries that are similar in structure but using INSERT operation. Other queries that execute only SELECT also work fine.

To repeat the problem in a test case, try these queries:

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();

Note. I run these queries from within the PHP environment, and I did NOT use mysql_select_db ('test') because I prefer not to do this, and none of the other (many!) Queries require this. I am sure that using mysql_select_db will solve my problem, but I would like to know why this particular request is not working.

: , mysql_select_db, :

update test.object1 set total=1, weight=1, dt=now() where id_object1=1;

. , , , : http://bugs.mysql.com/bug.php?id=28551 () ...

+5
3

, , MySQL, , .

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_object1, lur.id_object1, date(lur.dt)
                order by
                        lur.id_object1, lur.id_object1
                ) 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();

UPDATE (SELECT )

+2

UPDATE -

  • s.object? s.id_object2?
  • lur.object1? lur.id_object1?
  • lur.object2? lur.id_object2?
  • ur.id_object ?

, -)


, script, . :

1 row inserted [0,184s]
1 row inserted [0,068s]
1 row inserted [0,066s]
1 row inserted [0,147s]
1 row inserted [0,060s]
Error (32,1): No database selected

, .

+1

, , "" "MyISAM". , , InnoDB, , , InnoDB.

, , , . , InnoDB, .

0
source

All Articles