根据select语句编写SQL删除

时间:2016-03-22 10:04:21

标签: mysql sql

如何在一个查询中进行以下查询和删除?

select krps.kpi_results_fk from report.kpi_results_per_scene krps inner join report.kpi_results kr on kr.session_uid = '0000c2af-1fc8-4729-bb2a-d4516a63107a' 
and kr.pk = krps.kpi_results_fk

delete from report.kpi_results_per_scene where kpi_results_fk = 'answer from above query'

3 个答案:

答案 0 :(得分:1)

使用IN运算符:

delete from report.kpi_results_per_scene where kpi_results_fk in (
select krps.kpi_results_fk from report.kpi_results_per_scene krps inner join report.kpi_results kr on kr.session_uid = '0000c2af-1fc8-4729-bb2a-d4516a63107a' 
and kr.pk = krps.kpi_results_fk)

答案 1 :(得分:1)

我认为对于您的情况, NO 需要使用inner join

以下查询可以减少inner join

的开销
DELETE FROM report.kpi_results_per_scene 
WHERE kpi_results_fk IN 
        (SELECT kr.pk FROM report.kpi_results kr 
            WHERE kr.session_uid = '0000c2af-1fc8-4729-bb2a-d4516a63107a') 

答案 2 :(得分:0)

尝试下面的代码:

delete from report.kpi_results_per_scene
where kpi_results_fk IN (select krps.kpi_results_fk
                        from report.kpi_results_per_scene krps inner join report.kpi_results kr
                        on kr.session_uid = '0000c2af-1fc8-4729-bb2a-d4516a63107a' 
                        and kr.pk = krps.kpi_results_fk)