Php MySQL“;错误1046(3D000):未选择任何数据库;关于更新查询
我有一个更新查询,其中我明确地引用了数据库,但MySQL仍然抱怨消息:Php MySQL“;错误1046(3D000):未选择任何数据库;关于更新查询,php,mysql,database,Php,Mysql,Database,我有一个更新查询,其中我明确地引用了数据库,但MySQL仍然抱怨消息:ERROR 1046(3D000):未选择任何数据库 其他结构类似但使用插入的查询可以正常工作。其他只执行选择的查询也可以正常运行 要在测试用例中重复此问题,请尝试运行以下查询: create table test.object1 ( id_object1 int unsigned not null auto_increment, total int, weight int, dt dateti
ERROR 1046(3D000):未选择任何数据库
其他结构类似但使用插入的查询可以正常工作。其他只执行选择的查询也可以正常运行
要在测试用例中重复此问题,请尝试运行以下查询:
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\u select\u db可以解决我的问题,但我想知道为什么这个特定的查询不起作用
为了便于比较:如果您运行这个更简单的查询,也不使用mysql\u select\u db,那么一切正常:
update test.object1 set total=1, weight=1, dt=now() where id_object1=1;
我找了也没用。我发现唯一接近的是这个bug报告:尤其是最后一条(未答复的)消息…在UPDATE语句中,您有一些错误的字段名-
- 什么是
?它不应该是s.object
s.id\u object2
- 什么是
?它不应该是lur.object1
lur.id\u object1
- 什么是
?它不应该是lur.object2
lur.id\u object2
- 最后的
ur.id\u对象是什么
我第一次运行这个脚本时就犯了那个错误。我的输出:
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
当我设置默认数据库名称时,问题就消失了。您的字段名称不正确,但即使您纠正了它们,这也是
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
或单嵌套查询或默认数据库工作正常)请记住,当引擎设置为MyISAM时,不能使用外键。不仅您在中创建外键的表需要是InnoDB,而且您从中获取键的表也需要是InnoDB
我和你犯了同样的错误,在我想到这一点之前,我已经把头发拔了好几天了。我检查了我的每个表,确保每个表的引擎都设置为InnoDB,现在我设置外键没有问题。这可能只是第一个…这听起来可能很傻,但你能用随机手动日期替换“now()”然后再试一次吗?@Sebas我刚刚做了,没有区别。这有什么关系?不,正如我所说的,这只是一个愚蠢的想法,这个函数调用一些内部构件,在您的情况下,会导致db问题。。。你知道,有时候当你在寻找问题时失去了4小时,实际上是这样愚蠢的事情我假设表名和列名之间的混乱(
object1
vsid\u object1
等等)仅仅是一个印刷错误?哎呀,我的错。你说得对,我在将代码转换为这个示例时犯了一些错误。现在修好了。还是一样的问题。