使用mysql从一个查询中删除多个表中的行

时间:2016-02-09 16:08:02

标签: mysql sql

我想要删除表dispatch,scormvars和licencias的行,但我不确定是不是: 我有一个选择,但我不知道用delete执行查询:

<receiver android:name=".MyReceiver">
    <intent-filter>
        <action android:name="android.intent.action.MEDIA_EJECT" />
        <action android:name="android.intent.action.MEDIA_REMOVED" />
        <action android:name="android.intent.action.MEDIA_MOUNTED" />
        <action android:name="android.intent.action.MEDIA_BAD_REMOVAL" />
        <data android:scheme="file" />
    </intent-filter>
</receiver>

2 个答案:

答案 0 :(得分:1)

您可以编写存储过程来封装所有步骤,因为在单个查询中无法从少数表中删除行。
您还可以编写AFTER DELETE触发器,并在删除适当的行时从一个表中删除行。
此外,您可以创建外键约束...

但所有这些选择都很复杂......

至于我,我认为更简单的方法是使用临时表来存储id,然后使用此临时表删除适当的行。例如:

if ($(window).width() < 500) {
   $("#two").prependTo(".grid");
} 

答案 1 :(得分:0)

我会在数据库未使用时调用该过程。会是这样的吗?

create procedure prueba()
begin
        create temporary table idsToDelete
            select
            distinct l.ID_plataforma as plataforma,
            s.ID_dispatch as dispatch,
            l.ID_licencia as licencia 
            from scormvars s
            left join dispatch d on s.ID_dispatch = d.ID_dispatch
            left join licencias l on d.ID_licencia = l.ID_licencia
            where l.ID_plataforma in (select ID_plataforma from borrarplataformas);

            delete from dispatch where ID_dispatch in (select dispatch from idsToDelete);
            delete from licencias where ID_licencia in (select licencia from idsToDelete);
            delete from plataformas where id_plataforma in (select plataforma from idsToDelete);
            delete from scormvars where ID_dispatch in (select dispatch from idsToDelete);
end