进入(用RETURNING子查询删除)

时间:2019-04-05 14:46:00

标签: mysql mariadb

假设我有2张桌子。

message_id中的列:segment_idsegments,...

segment_id中的列:message_id,...

我只需要基于DELETE FROM segments WHERE segment_id in (select segment_id FROM submissions WHERE message_id = p_message_id); DELETE FROM submissions WHERE message_id = p_message_id; 删除两个表中的记录。

我知道我可以做这样的事情:

DELETE
    FROM
        segments
    WHERE
        segment_id in (DELETE FROM submissions WHERE message_id = p_message_id RETURNING segment_id);

但是我想知道是否有更优雅的(性能明智的)方法可以一次性完成这样的事情?

DELETE FROM submissions WHERE message_id = p_message_id RETURNING segment_id;

如果我自己运行“删除”子查询,它会正常工作,并返回已删除的segment_id:

Public Sub readopentextfile()
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim oFS, oFSO, oFSW
Dim stext As String
Set oFSO = CreateObject("Scripting.FileSystemObject")

'/ Open new DailySalesOrds for reading
Set oFS = oFSO.OpenTextFile("C:\DailySalesOrds.csv", ForReading)

'/ Open exception file for writing
Set oFSW = oFSO.OpenTextFile("C:\DailySalesOrderExcep.csv", ForAppending)

'/ Read each record and count commas if 21 or more add record to exception file
Do Until oFS.AtEndOfStream
         stext = oFS.ReadLine
    Do
    c = InStr(c + 1, stext, ",")
        If c <> 0 Then Count = Count + 1
    Loop Until c = 0

'Debug.Print Count
    If Count >= 21 Then
       oFSW.Write (vbNewLine & stext)
    End If
c = 0
Count = 0
Loop
End Sub

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

如果您在DELETE子句中指定了两个表,则可以一次将其删除,所以我认为您只需要这样的内容:

DELETE s, si
FROM segments s INNER JOIN segment_id si
  ON s.message_id = si.message_id
WHERE s.message_id = p_message_id

a number of examples here.