临时 table 更新 mysql
temporay table update mysql
我正在将我的存储过程从 Sql Server
迁移到 MySql
。
我在某个地方遇到了将更新查询从 MS sql 服务器转换为临时变量 Mysql 的问题。这是我的 MS sql 服务器查询:
update sampleStoreTV set felony = c.fel,misdemeanor = c.mis,violation = c.vio
from (select srcCL.id as 'storeId',
case when SUM(case when srcCL.cl='FELONY' then 1 else 0 end)>0 then 1 else 0 end as 'fel',
case when SUM(case when srcCL.cl='MISDEMEANOR' then 1 else 0 end)>0 then 1 else 0 end as 'mis',
case when SUM(case when srcCL.cl='VIOLATION' then 1 else 0 end)>0 then 1 else 0 end as 'vio'
from
(select DM_Sample_Store.DM_Sample_Store_ID as 'id',
Charge_Level.Mapped_Charge_Level as 'cl'
from DM_Sample_Store
left join screening_result on Screening_Result.Screening_Result_id = DM_Sample_Store.Screening_Result_id
left join Crim_Case on Screening_Result.Screening_Result_id = Crim_Case.Screening_Result_id
left join Crim_Charge on Crim_Case.Crim_case_id = Crim_Charge.Crim_case_id
left join PDL_DataSource on DM_Sample_Store.DataSource_Id = PDL_DataSource.DataSource_Id
left join DM_Sample_Search_Param on DM_Sample_Store.DM_Sample_Store_ID = DM_Sample_Search_Param.DM_Sample_Store_ID
left join Charge_Level on Crim_Charge.Sanitized_Charge_Level_id = Charge_Level.Charge_Level_id
where PDL_DataSource.DataSource_Id = dataSourceId
and DM_Sample_Store.Active = 1
and DM_Sample_Search_Param.Global_Search_Parameter_ID in (sosGspId, freqGspId, clGspId, dtGspId)
and DM_Sample_Search_Param.Active = 1
-- and Crim_Charge.AOR_Days is not null
) srcCL
group by id
having SUM(case when cl='FELONY' then 1 else 0 end)>0
or SUM(case when cl='MISDEMEANOR' then 1 else 0 end)>0
or SUM(case when cl='VIOLATION' then 1 else 0 end)>0) c
where sampleId = c.storeId
我不确定您要做什么,但是由于多种不同的原因,您的查询是完全错误的。
您需要将您的 sampleStoreTV 与您的其他桌子连接起来。我想可能是这样的:
update sampleStoreTV
inner join (your query) as c on sampleStoreTV.sampleId = c.storeId
set sampleStoreTV.felony = c.fel,
sampleStoreTV.misdemeanor = c.mis,
sampleStoreTV.violation = c.vio
希望对你有帮助
我正在将我的存储过程从 Sql Server
迁移到 MySql
。
我在某个地方遇到了将更新查询从 MS sql 服务器转换为临时变量 Mysql 的问题。这是我的 MS sql 服务器查询:
update sampleStoreTV set felony = c.fel,misdemeanor = c.mis,violation = c.vio
from (select srcCL.id as 'storeId',
case when SUM(case when srcCL.cl='FELONY' then 1 else 0 end)>0 then 1 else 0 end as 'fel',
case when SUM(case when srcCL.cl='MISDEMEANOR' then 1 else 0 end)>0 then 1 else 0 end as 'mis',
case when SUM(case when srcCL.cl='VIOLATION' then 1 else 0 end)>0 then 1 else 0 end as 'vio'
from
(select DM_Sample_Store.DM_Sample_Store_ID as 'id',
Charge_Level.Mapped_Charge_Level as 'cl'
from DM_Sample_Store
left join screening_result on Screening_Result.Screening_Result_id = DM_Sample_Store.Screening_Result_id
left join Crim_Case on Screening_Result.Screening_Result_id = Crim_Case.Screening_Result_id
left join Crim_Charge on Crim_Case.Crim_case_id = Crim_Charge.Crim_case_id
left join PDL_DataSource on DM_Sample_Store.DataSource_Id = PDL_DataSource.DataSource_Id
left join DM_Sample_Search_Param on DM_Sample_Store.DM_Sample_Store_ID = DM_Sample_Search_Param.DM_Sample_Store_ID
left join Charge_Level on Crim_Charge.Sanitized_Charge_Level_id = Charge_Level.Charge_Level_id
where PDL_DataSource.DataSource_Id = dataSourceId
and DM_Sample_Store.Active = 1
and DM_Sample_Search_Param.Global_Search_Parameter_ID in (sosGspId, freqGspId, clGspId, dtGspId)
and DM_Sample_Search_Param.Active = 1
-- and Crim_Charge.AOR_Days is not null
) srcCL
group by id
having SUM(case when cl='FELONY' then 1 else 0 end)>0
or SUM(case when cl='MISDEMEANOR' then 1 else 0 end)>0
or SUM(case when cl='VIOLATION' then 1 else 0 end)>0) c
where sampleId = c.storeId
我不确定您要做什么,但是由于多种不同的原因,您的查询是完全错误的。
您需要将您的 sampleStoreTV 与您的其他桌子连接起来。我想可能是这样的:
update sampleStoreTV
inner join (your query) as c on sampleStoreTV.sampleId = c.storeId
set sampleStoreTV.felony = c.fel,
sampleStoreTV.misdemeanor = c.mis,
sampleStoreTV.violation = c.vio
希望对你有帮助