MySQL Workbench动作输出中的MySQL错误代码1146

时间:2015-07-25 17:11:02

标签: sql mysql-error-1146

我刚开始学习SQL。当我写这段代码时

create database tutorial_1;
use tutorial_1;
create table tutorial
(
id int primary key,
name varchar(40),
duration decimal(4,2),
date date
);
insert into tutorial(id, name, duration, date)
values(1, 'c', 23.12, '2014-02-10');
insert into tutorial
values(2, 'python', 12.45, '2014-02-11');
insert into tutorial
values(3, 'sql', 34.17, '2014-02-14');

create database tutorial_2;
use tutorial_2;
create table tutorial_info
(
tutorial_id int primary key,
views int(40),
likes int(40),
dislikes int(40),
shares int(40)
);
insert into tutorial_info(tutorial_id, views, likes, dislikes, shares)
values (1, 23411, 124, 12, 19);
insert into tutorial_info(tutorial_id, views, likes, dislikes, shares)
values (2, 29019, 920, 100, 234);
insert into tutorial_info
values (3, 9019, 129, 23, 8);
insert into tutorial_info

SELECT t.id, t.name,
ti.views, ti.likes, ti.dislikes, ti.shares
FROM tutorial AS t
JOIN tutorial_info AS ti
ON t.id=ti.tutorial_id;

在SQL文件上,操作输出显示:

  

错误代码:1146。表' tutorial_2.tutorial'不存在0.000秒

但是当我在SCHEMAS上查看表格时,我发现了那张桌子。我该怎么做才能解决它?

2 个答案:

答案 0 :(得分:1)

tutorial是数据库tutorial_1的表,而您正在数据库tutorial2的范围内工作(use tutorial_2之后)。所以你应该写:

SELECT t.id, t.name,
ti.views, ti.likes, ti.dislikes, ti.shares
FROM tutorial_1.tutorial AS t
JOIN tutorial_info AS ti
ON t.id=ti.tutorial_id;

通常,您应该只使用一个数据库。

答案 1 :(得分:1)

您正在创建两个单独的数据库:tutorial_1tutorial_2

然后,切换到tutorial_2数据库。之后,您正在选择tutorialtutorial_info之间的联接。 tutorial位于tutorial_1数据库中,而tutorial_info位于tutorial_2数据库中。由于您位于tutorial_2数据库中,MySQL会尝试从tutorial表中加载记录,而tutorial_2数据库中不存在这些记录,因为它已创建到tutorial_1数据库中

编辑: 我进行了一些实验,我相信你可以像这样进行跨数据库连接:

SELECT t.id, t.name,
ti.views, ti.likes, ti.dislikes, ti.shares
FROM tutorial_1.tutorial AS t
JOIN tutorial_info AS ti
ON t.id=ti.tutorial_id;