[英]How to join two rows from a mysql table into a third one
what I have to do is basiclly create 3 tables. 我要做的基本上是创建3个表。
I have created the first two which are the following. 我创建了下面的前两个。
table 1 表格1
table 2 表2
Table 3 has to be this 表3必须是这个
What you need is a FOREIGN KEY with a ON UPDATE CASCADE definition, like this: 您需要的是带有ON UPDATE CASCADE定义的FOREIGN KEY,如下所示:
create table table_3 (
user_id int(10) unsigned,
CONSTRAINT fk_tb_1_user_id FOREIGN KEY (user_id) REFERENCES table_1(user_id) ON UPDATE CASCADE
);
int(10) unsigned is a type I invented to exemplify, but it should match exactly the user_id column type in table_1. 我发明了int(10)unsigned来举例说明这种类型,但它应该与table_1中的user_id列类型完全匹配。
But pay attention, for the FOREIGN KEY to work, every user_id in table_3 MUST exist in table_1, you will not be able to insert a user_id in table_3 if it doesn't exist in table_1. 但是请注意,为了使FOREIGN KEY起作用,table_3中的每个user_id必须存在于table_1中,如果table_3中不存在一个user_id,则将无法在其中插入一个user_id。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.