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