临时表更新mysql

时间:2017-02-28 10:54:38

标签: mysql sql-server mysql-error-1064

我正在将我的存储过程从Sql Server迁移到MySql

我正面临着将更新查询从MS sql server转换为Mysql以获取临时变量的问题。这是我的MS sql server查询:

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

1 个答案:

答案 0 :(得分:1)

我不确定你要做什么,但是由于许多不同的原因你的查询完全错误。

您需要将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

我希望它有所帮助