postgresql:删除表中的行,这些行通过外键匹配另一个表中的值

时间:2018-10-01 11:21:44

标签: postgresql

我有两个表:event_to_state_groupsevents

我可以通过

选择要在event_to_state_groups中删除的所有条目
SELECT events.event_id, room_id, sender 
FROM event_to_state_groups, events
WHERE event_to_state_groups.event_id = events.event_id 
  AND events.room_id like '%jGUBNvBzjGyQyKKMJV:matrix.org'

如何从event_to_state_groups表中删除所有与%jGUBNvBzjGyQyKKMJV%表中的room_id events连接的条目?

2 个答案:

答案 0 :(得分:0)

检查

   delete FROM events using event_to_state_groups 
    WHERE event_id=event_to_state_groups.event_id AND room_id like '%jGUBNvBzjGyQyKKMJV%'

答案 1 :(得分:0)

尝试一下:

delete from event_to_state_groups where room_id 
  IN (select e.room_id from event_to_state_groups e 
      INNER JOIN events ev on e.event_id = ev.event_id 
      WHERE e.room_id ilike '%jGUBNvBzjGyQyKKMJV%')