gpt4 book ai didi

mysql - 如何使用来自不同表的查询结果填充一个表?

转载 作者:行者123 更新时间:2023-11-30 01:19:14 25 4
gpt4 key购买 nike

我创建了一个查询,该查询可以工作并提供我想要的内容,但它不会在批处理文件中执行。如何使用成功执行的查询的结果填充另一个表?

这是我的查询

select  

isnull (start_charge.account,'No account at start') Start_charge_account
,Finish_charge.account start_charge_account
,case when start_charge.account is null then finish_charge.account else start_charge.account end "account"
,isnull (start_charge.Branch,99999999999) Start_charge_Branch
,Finish_charge.Branch Finish_charge_Branch
,case when start_charge.branch is null then finish_charge.branch else start_charge.branch end " branch"
,isnull (start_charge. site_number,99999999999) Start_charge_site_number
,Finish_charge. site_number Finish_charge_site_number
,case when start_charge.site_number is null then finish_charge.site_number else start_charge.site_number end "site_number"
,isnull (start_charge. service_code, 'No account at start') Start_charge_service_code
,Finish_charge. service_code finish_charge_service_code
,isnull (start_charge. item_code,'No account at start') Start_charge_item_code
,Finish_charge. item_code finish_charge_item_code
,isnull (start_charge. why_code, 'No account at start') Start_charge_why_code
,Finish_charge. why_code finish_charge_why_code
,isnull (start_charge. why_type, 'No account at start') Start_charge_why_type
,Finish_charge. why_type finish_charge_why_type

,Start_charge.Start_charge
,isnull(Finish_charge.Finish_charge,0) Finish_charge
,isnull(Start_charge.Start_charge,0)-isnull(Finish_charge.Finish_charge,0) Variance


from

(
select

account
,branch
,site_number
,service_code
,item_code
,why_code
,why_type
,sum(charge)
"Start_Charge"

from

cannon_commercial

where

1=1
and record_type = 'PS'
and period = ' 02/07/2013'

group by

account
,branch
,site_number
,service_code
,item_code
,why_code
,why_type
) start_charge



full outer join



( select

account
,branch
,site_number
,service_code
,item_code
,why_code
,why_type
,concat(account,site_number) "account - site"
,ISNULL(sum(charge),0) "Finish_Charge"

from

cannon_commercial

where

1=1
and record_type= 'PS'
and period = ' 04/06/2013'

group by

account
,branch
,site_number
,service_code
,item_code
,why_code
,why_type
)
Finish_charge

on Start_charge.Account = Finish_charge.Account
and Start_charge.Branch = Finish_charge.Branch
and Start_charge.site_number = Finish_charge.site_number
and Start_charge.item_code = Finish_charge.item_code
and Start_charge.service_code = Finish_charge.service_code
and Start_charge.why_code = Finish_charge.why_code
and Start_charge.why_type = Finish_charge.why_type

最佳答案

有两种处理方法:

CREATE TABLE mytable AS
(SELECT ... )

将使用查询结果创建一个新表。

INSERT INTO mytable (field1, field2, ...)
(SELECT ... )

会将查询结果插入到现有表中。当然,您需要确保列列表匹配。如果查询返回的列列表与您现有的表完全匹配,那么您不需要指定字段列表,您的语句就会变得简单

INSERT INTO mytable (SELECT ... )

关于mysql - 如何使用来自不同表的查询结果填充一个表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18737179/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com