sql将查询结果保存为新表

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql将查询结果保存为新表相关的知识,希望对你有一定的参考价值。

sql将查询结果保存为新表
create table xx (select atmjyje.jgm,atmjyje.jgmc,atmjyje.jyje,atmjybs.jybs,atmjyje.jyje/atmjybs.jybs as mbjybs from atmjybs inner join atmjyje
on atmjyje.jgm=atmjybs.jgm and atmjyje.jgmc=atmjybs.jgmc)
麻烦看下这段话哪错了?
那where 后面该加点什么呢
select
atmjyje.jgm,atmjyje.jgmc,atmjyje.jyje,atmjybs.jybs,atmjyje.jyje/atmjybs.jybs as mbjybs
into atmbjyje
from atmjybs inner join atmjyje
on atmjyje.jgm=atmjybs.jgm and atmjyje.jgmc=atmjybs.jgmc
这是我的答案,可以建表,不过是临时的.

参考技术A atmjyje.jyje/atmjybs.jybs 这里多了一个“/”少了一个“,”

前面也不对,例:
select * into xx from (select atmjyje.jgm,atmjyje.jgmc,atmjyje.jyje,atmjybs.jybs,atmjyje.jyje,atmjybs.jybs as mbjybs from atmjybs inner join atmjyje
on atmjyje.jgm=atmjybs.jgm and atmjyje.jgmc=atmjybs.jgmc) b
参考技术B create table new_table_name as
select * from old_table_name;
可以查询几个字段,这样的话就可以把*换成需要的字段,当然也可以加where条件查询需要的数据。
参考技术C 这段语句里怎么还有个 反斜线? jyje/atmjybs.jybs as
而且没发现 where ...

创建表的时候应该判断这个表是否已经 存在,即使你创建的 是临时表,
参考技术D create table xx (select atmjyje.jgm,atmjyje.jgmc,atmjyje.jyje,atmjybs.jybs,atmjyje.jyje/atmjybs.jybs as mbjybs from atmjybs inner join atmjyje
on atmjyje.jgm=atmjybs.jgm WHERE atmjyje.jgmc=atmjybs.jgmc)

AND -> where

sql中引用一个表的查询结果作为条件来查询另一个表如何实现?

我自己想了下大概是这样的:

select 消费号,sum(金额)金额 from 护理卡消费
where exists (
select distinct a.消费号 from 护理卡消费历史 a ,护理卡消费 d
where a.服务名称 in (select b.服务名称 from 服务类别 b where b.服务项目类别='面部护理')
and a.消费号=d.消费号
)
group by 金额,消费号
with rollup
可是结果却不对,以上查询后把所有的内容都查询出来了,并没有根据子查询里面选择出来的消费号,进行外表查询,求如何实现?

sql中引用一个表的查询结果作为条件来查询另一个表称为sql嵌套查询。

简单子查询

示例:Copyselect name,age from person 

where age > 

   (        select age from person 

       where name = '孙权'

   )

in嵌套查询

in关键字用于where子句中用来判断查询的表达式是否在多个值的列表中。返回满足in列表中的满足条件的记录。

示例:

select name from person 

 where countryid in 

(

select countryid from country

where countryname = '魏国'

)

扩展资料

子查询的语法规则

1、子查询的select查询总是使用圆括号括起来。

2、不能包括compute或for.browse子句。

3、如果同时指定top子句,则可能只包括order by子句。

4、子查询最多可以嵌套到32层。个别查询可能会不支持32层嵌套。

5、任何可以使用表达式的地方都可以使用子查询,只要它返回的是单个值。

6、如果某个表只出现在子查询中二不出现在外部查询中,那么该表的列就无法包含在输出中。

子查询的组成

1、包含标准选择列表组件的标准select查询。

2、包含一个或多个表或者视图名称的标准from子句。

3、可选的where子句。

4、可选的group by子句。

5、可选的having子句。

参考技术A

1、用sqlserver作为测试,创建学生、教师、班级三张表。每张表都有一个id,int类型的(自增长),作为每个表的主键。

2、添加测试数据,并创建班级与学生、教师与班级的中间表。insert into dbo.Student(Sname) values('张三'),插入多条,由于id自增长所以sid自动填充了。类似将教师和班级也添加上测试数据。

3、创建班级教师表Class_Teacher,班级学生表Class_Student。

4、然后将1和2 放到1班,3和4放到2班。5和6 不放(可以理解为刚入学没有分配班级)。然后将3个老师分配到3个班级insert into dbo.Class_Teacher values (1,1)insert into dbo.Class_Teacher values (2,2)insert into dbo.Class_Teacher values (3,3)。

5、这样,1班和2班各有两名同学,3班没有同学,有两个同学没有分配班级,每一个老师都分配了班级。现在要查询所有班级学生情况。

参考技术B exists 这个里面得要包含和外面表的关系的。

select 消费号,sum(金额)金额 from 护理卡消费 T
where exists (
select distinct a.消费号 from 护理卡消费历史 a left join 护理卡消费 d on a.消费号=d.消费号
where a.服务名称 in (select b.服务名称 from 服务类别 b where b.服务项目类别='面部护理')
and t.消费号=a.消费号 )
group by 金额,消费号
参考技术C exists 这个里面得要包含和外面表的关系的。

select 消费号,sum(金额)金额 from 护理卡消费 T
where exists (
select distinct a.消费号 from 护理卡消费历史 a left join 护理卡消费 d on a.消费号=d.消费号
where a.服务名称 in (select b.服务名称 from 服务类别 b where b.服务项目类别='面部护理')
and t.消费号=a.消费号 )
group by 金额,消费号

你这样试试本回答被提问者采纳

以上是关于sql将查询结果保存为新表的主要内容,如果未能解决你的问题,请参考以下文章

如何将选择 sql 查询的结果转换为 ms 访问中的新表

SQL保存查询结果——生成新表

SQL保存查询结果——生成新表

sql server怎么导出查询结果为脚本

sql中引用一个表的查询结果作为条件来查询另一个表如何实现?

sql 查询结果存储到变量