Search code examples
mysqlsql-servermysql-error-1064

temporay table update mysql


I am migrating my stored procedures from Sql Server to MySql.

Somewhere I am facing problem to transform update queryfrom MS sql server to Mysql for temporary variable. Here is my MS sql server query:

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

Solution

  • I am not sure what youre trying to do but your query is completely wrong for many different reasons.

    You need to join your sampleStoreTV with your other tables. I guess it might be something like that:

    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
    

    I hope it helps