Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Mysql中使用什么来实现完整的合并表_Mysql_Database_Mysql Workbench - Fatal编程技术网

在Mysql中使用什么来实现完整的合并表

在Mysql中使用什么来实现完整的合并表,mysql,database,mysql-workbench,Mysql,Database,Mysql Workbench,我正在创建两个表 表1有以下模式 user_id int not null,autoincrement movie _id int not null movie_name varchar user_name varchar rating int genre varchar movie _id int not null movie_name

我正在创建两个表

表1有以下模式

user_id       int            not null,autoincrement
movie _id     int            not null 
movie_name    varchar        
user_name     varchar
rating        int
genre         varchar
movie _id     int            not null 
movie_name    varchar        
user_name     varchar
genre         varchar
rating        varchar
而表2有以下模式

user_id       int            not null,autoincrement
movie _id     int            not null 
movie_name    varchar        
user_name     varchar
rating        int
genre         varchar
movie _id     int            not null 
movie_name    varchar        
user_name     varchar
genre         varchar
rating        varchar
现在,当我将查询放入插入值时,它首先检查第一个表中是否存在以下用户名。如果为true,则它将插入到第二个表中,否则它将根据架构将值插入到第一个表中。换句话说,第一个表具有唯一的用户名和唯一的用户名,而第二个表包含许多重复的用户名,它们具有相同的名称看见

因此,我通过表单(Java、servlet)将以下值插入到我的表中

user_Id    movie_Id    movie_name      user_name   rating  genre
1           1           Twister         Alex          6      Drama
2           1           Twister         Tim           1      Drama
3           2           sweet november  pam           5      Romantic
4           3           The pianist     carl          5      Drama 
5           4           narnia          stephen       7      Fantasy   
..
..
(contd..)
表2

    movie_Id   movie_Name    user_name     genre     Rating
    2          sweet november  Alex        Romantic    4
    3          The pianist     Alex        Drama       5
    4          narnia          Pam         Fantasy     8
    9          maceth          Tim         Drama       9
    ..
    ....

(contd.)
。。 。此外,我想合并这两个表,以便得到下图

user_id   movie_Id  movie_name      user_name   rating  genre
1         1         Twister            Alex        6      Drama
1         2         sweet november     Alex        4      Romantic
1         3         The pianist        Alex        5      Drama
2         1         Twister            Tim         1      Drama
2         9         macbeth            Tim         9      Drama
3         2         Sweet November     Pam         5      Romatic
3         4         Narnia             Pam         8      Fantasy
4         3         The Pianist        Carl        5      Drama
5         4         Narnia             Stephen     7      Fantasy
... and so on 

What should I use
我尝试使用join,但它忽略了第一个表值。我希望在表单中输入值,然后单击后立即使用这两个表值

下面是我使用的语法

select * from table2 inner join table1 on table2.user_name = table1.user_name
请提出一些建议


谢谢

您的数据设计非常糟糕。例如,表2和表1之间没有明显的联系。它应该有一个
user\u id
列,而不是
user\u name

不过,您的问题的答案是
union all
,而不是
join
(或者更确切地说,除了)。您需要一个联接来查找第二个表的
用户id

select user_id, movie_Id, movie_name, user_name, rating, genre
from Table1 union all
select t1.user_id, t2.movie_Id, t2.movie_name, t2.user_name,  t2.rating, t2.genre
from table2 t2 join
     table1 t1
     on t2.user_name = t1.user_name;

也就是说,您应该修改数据库结构。作为提示,它应该有三个表:
users
movies
、和
user\u movies

您的表结构很难理解。
table1
table2
应该代表什么?除了多余的
user\u id
标记外,唯一不在这两个表上的字段是
Rating
Hi Doug M是的,你是对的,但我试图实现的唯一一件事是使合并表连接或合并这两个表Hi Eggyal,是的,你是对的,我仍然无法获得所需的结果。您只能通过
内部联接从表2中获得结果,因为它只会在表1中显示与表2中用户名匹配的结果。如果要在一个查询中从两个表中选择所有数据,则需要一个
联合。