insert into select查询疑问

guaili1752 2017-06-01 10:42:20
table1查询结果
select id ,name,age from table1 where...
id name age
1 Nick 30
2 Emma 26
table2查询结果
select id ,position from table2 where...
id position
1 Teacher
2 Doctor
3 Doctor
现在要将这两个查询结果合并在一起,id一致的才能合并在同一行上
id name aged position
1 Nick 30 Teacher
2 Emma 26 Doctor
查询语句是这么写的么
Insert into (select id ,name,age from table1 where...)(potion) select id ,position from table2 where...where table1.id=table2.id
table1和table2的查询结果不想在insert into select前查询出,那样的话还要重新建表
...全文
202 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
正怒月神 2017-06-01
  • 打赏
  • 举报
回复
mysql和mssql语法差不多

select * from table1 a
INNER JOIN table2 b on a.id=b.id
  • 打赏
  • 举报
回复
引用 3 楼 guaili1752 的回复:
[quote=引用 1 楼 duanzi_peng 的回复:] Insert into 后边要有表名称 ,你可以把结果放到临时表里,既然是insert 就要有对应的列。
select name,age into #temp1 from table1 where... select position into #temp2 from table2 where... insert into #temp1(....) select * form #temp2 这样的吗?但是MySqL是不是不支持select into语句 [/quote] 不清楚mySql,问题描述里也没说是mysql数据库。
guaili1752 2017-06-01
  • 打赏
  • 举报
回复
引用 1 楼 duanzi_peng 的回复:
Insert into 后边要有表名称 ,你可以把结果放到临时表里,既然是insert 就要有对应的列。
select name,age into #temp1 from table1 where... select position into #temp2 from table2 where... insert into #temp1(....) select * form #temp2 这样的吗?但是MySqL是不是不支持select into语句
guaili1752 2017-06-01
  • 打赏
  • 举报
回复
引用 1 楼 duanzi_peng 的回复:
Insert into 后边要有表名称 ,你可以把结果放到临时表里,既然是insert 就要有对应的列。
怎么把查询结果给临时表
  • 打赏
  • 举报
回复
Insert into 后边要有表名称 ,你可以把结果放到临时表里,既然是insert 就要有对应的列。

111,093

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧