SQL:插入...值..选择

SQL: INSERT INTO...VALUES..SELECT(SQL:插入...值..选择)
本文介绍了SQL:插入...值..选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我从 TableX 中获取 colA、从 TableY 中获取 colB 以及从 TableZ 中获取 colC 的值,我该如何编写 INSERT 语句?

How do i write INSERT statement if i get the values of colA from TableX, colB from TableY and colC from TableZ?

例如:INSERT INTO TableA (colA, colB, colC) VALUES (?,?,?)

eg: INSERT INTO TableA (colA, colB, colC) VALUES (?,?,?)

如果可能的话,有什么想法吗?

Any ideas if it is possible?

推荐答案

INSERT INTO TableA(colA, colB, colC)
  SELECT TableX.valA, TableY.valB, TableZ.valC
    FROM TableX
   INNER JOIN TableY ON :......
   INNER JOIN TableZ ON ........

当然,TableX、TableY 和 TAbleZ 也可能以其他方式相关(不是 INNER JOIN).

Of course, TableX, TableY and TAbleZ might also be related in some other way (not INNER JOIN).

如果你根本找不到表之间的任何关系,你也可以做三个单独的

If you cannot find any relation between the tables AT ALL, you could also do three separate

SELECT @value1 = valA FROM TableX WHERE ......
SELECT @value2 = valB FROM TableY WHERE ......
SELECT @value3 = valC FROM TableZ WHERE ......

然后是这样的插入:

INSERT INTO TableA(colA, colB, colC)
             VALUES(@value1, @value2, @value3)

这是最后的最后手段,你不能在单个 SELECT 语句中表达所有内容.

That's the ultimate last resort, you can can't express everything in a single SELECT statement.

马克

这篇关于SQL:插入...值..选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Execute complex raw SQL query in EF6(在EF6中执行复杂的原始SQL查询)
Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)