简体   繁体   English

如何将MySQL表中的两行连接到第三行

[英]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

    • User_id 用户身份
    • email 电子邮件
    • Password 密码
  • table 2 表2

    • Schedule_id Schedule_id
    • Time 时间
    • Date 日期
  • Table 3 has to be this 表3必须是这个

    • User_id <<<< that user_id has to be the same as the one mentioned in table number one, where if I was to change the user ID in table 1 it would also change in table number 3 User_id <<<<该user_id必须与表1中提到的用户ID相同,如果要更改表1中的用户ID,它也将在表3中进行更改
    • number

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.

相关问题 从两个表联接,然后从结果表联接到第三个表mysql - Join from two table and then join from the resultant to third table mysql mysql join查询从两个表中选择行,一个表有多个与第一个表匹配的行 - mysql join query for select rows from two tables that one table is having multiple rows matching to the first table Laravel Mysql从两个表中选择并在第三个表上连接结果 - Laravel Mysql Select from two tables and join result on a third table MySQL连接两个表,一个表具有多个匹配行 - mysql join two tables that one table is having multiple matching rows 使用Codeigniter将表中的一行与MySQL中其他两个表的多行联接 - Join one row from a table with multiple rows of two other tables in MySQL using Codeigniter MySQL两表INNER JOIN,LEFT JOINED到第三表,只有一行的最小值 - MySQL two-table INNER JOIN , LEFT JOINED onto third table with only one row with lowest value PHP mysql如何连接两个表来从一个表链接名称和从其他表发布 - PHP mysql how to join two tables to link name from one table and post from other table 使用联接从一个MYSQL表中选择随机行 - Select random rows from one MYSQL table with join 连接两个表,但排除匹配第三个表中条件的行 - Join two tables but exclude rows that match condition in a third table 如何从一张表中选择两行? - How to select two rows from one table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM