简体   繁体   English

MYSQL:如何根据其他两个表的条​​件在表中插入行

[英]MYSQL: How to insert rows in a table based on a condition of other two tables

The setup is this: 设置是这样的:

table A has columns desc and date 表A具有desc和date列

table B has columns desc, date and task 表B具有desc,日期和任务列

table C has columns keyid and type. 表C具有列keyid和类型。

What I want to do is to copy some of the rows from table B to table A, where A.desc should be B.desc and A.date should be B.date. 我要做的是将表B中的某些行复制到表A,其中A.desc应该是B.desc,而A.date应该是B.date。 To select which rows to copy, the logic is this: "All rows such that B.task = C.keyid AND C.type='1'". 要选择要复制的行,逻辑是:“所有使B.task = C.keyid AND C.type ='1'的行”。

If I wanted to SELECT only the rows to copy, the query would be this: 如果我只想选择要复制的行,查询将是这样的:

SELECT B.desc, B.date FROM B,C WHERE B.task=C.keyid AND C.type='1'

The rows resulting from that selection need to be copied to A. I know how to write the select, but how do I write the INSERT INTO query? 该选择产生的行需要复制到A。我知道如何编写选择,但是如何编写INSERT INTO查询?

这个未经测试的查询应该工作:

insert into tableA (tableA.desc, tableA.data) SELECT B.desc, B.date FROM B,C WHERE B.task=C.keyid AND C.type='1'

you can run this query: 您可以运行以下查询:

 INSERT INTO A(desc, date) 
    VALUES (
      SELECT B.desc, B.date 
       FROM B,C 
       WHERE B.task=C.keyid AND C.type='1' 
    )

I hope it will help. 希望对您有所帮助。

You can try this: 您可以尝试以下方法:

INSERT INTO table_A (desc, date)
SELECT B.desc, B.date FROM table_B as B,table_C as C WHERE B.task=C.keyid AND C.type='1'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MySQL根据一个表的条件将其插入两个表之一 - MySQL insert in one of two tables based on condition for one table 如何根据另外两个表的连接插入值 (MySQL) - How to insert values based on a join of two other tables (MySQL) 如何根据MySQL中的相关性将两个表的行相互映射? - How to map rows of two tables to each other based on the relevance in MySQL? 根据其他两个表的比较插入表中 - Insert into table based on comparison of two other tables 使用PDO根据mySQL中其他两个表的结果从表中选择行 - Select rows from a table based on results from two other tables in mySQL using PDO 如何根据日期条件(PHP-MySQL-Query)将一个表中的多行插入另一个表? - How to insert multiple rows from a table to another table based on date condition (PHP-MySQL-Query)? 如何基于其他3个表更新MySQL表 - How to update MySQL table based on 3 other tables MySQL:如何通过链接到其他两个表的两个外键插入到表中? - MySQL: how to insert into a table with two foreign keys linking to two other tables? msql根据另外两个表中的值将数据插入表中 - msql insert data into table based on values in two other tables 在MySQL中分别从另外两个表向表中插入行 - Insert rows into a table from another two tables separately in mysql
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM