MySQL“ERROR 1046(3D000):未选择数据库”更新查询

时间:2012-05-18 13:29:55

标签: php mysql database

我有一个UPDATE查询,我明确地引用了数据库,但MySQL仍然抱怨消息:ERROR 1046 (3D000): No database selected

其他类似结构的查询,但使用INSERT工作正常。其他只执行SELECT的查询也运行正常。

要在测试用例中重复此问题,请尝试运行以下查询:

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

注意:我正在从PHP环境运行这些查询,并且我没有明确地使用mysql_select_db('test'),因为我不喜欢和其他(很多!)查询都不需要它。我确信使用mysql_select_db可以解决我的问题,但我想知道为什么这个特定的查询不起作用。

为了比较起见:如果你运行这个更简单的查询,也没有使用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,尤其是最后一条(未答复的)消息......

3 个答案:

答案 0 :(得分:2)

您的字段名称不正确,但即使您更正了它们,这也是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或单嵌套查询或默认数据库工作正常)

答案 1 :(得分:1)

UPDATE语句中有一些错误的字段名称 -

  • 什么是s.object?不应该是s.id_object2
  • 什么是lur.object1?不应该是lur.id_object1
  • 什么是lur.object2?不应该是lur.id_object2
  • 最后ur.id_object是什么?

解决所有这些问题并尝试再次更新; - )


我第一次运行此脚本时遇到了错误。我的输出:

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

当我设置默认数据库名称时,问题就消失了。

答案 2 :(得分:0)

请记住,当Engine设置为MyISAM时,您无法使用外键。您创建外键的表不仅需要InnoDB,而且您获取密钥的表也需要是InnoDB。

在我想到这件事之前,我和你一样犯了同样的错误并把头发拉了几天。我进入了每个表格并确保每个表都设置为InnoDB,现在我没有设置外键的问题。