SQL删除类似的条目

时间:2017-04-23 03:19:21

标签: sql jdbc derby

SQL问题。 我目前有两个SQL表,一个名为Waitlist,一个名为Booking。它们不相同(等待列表包含的信息比预订更多)。他们看起来像这样:

候补:

#   Customer    Day                 Timestamp                   Flight
1   Joseph     2017-04-22      2017-04-22 16:20:33.178     F101

售票:

#   Customer    Flight  Day
1   Timmy   F101    2017-04-22
2   Joseph  F101    2017-04-22

我想从waitlist中删除重复数据(在本例中为joseph条目)....我尝试了以下内容: DELETE FROM Waitlist WHERE Customer = booking.customer and day = booking.day and flight = booking.flight 我也试过INNER JOIN而没有运气。 请帮助!!

1 个答案:

答案 0 :(得分:0)

有多种方法可以实现您的目标,但对您的问题最恰当的答案是数据库供应商特定

即。在MySQL和SQL Server中,您可以直接在JOIN语句

中使用DELETE
DELETE w 
  FROM waitlist w JOIN booking b 
    ON w.customer = b.customer
   AND w.day = b.day
   AND w.flight = b.flight; 

Sqlfiddle用于MySql,Sqlfiddle用于SQL Server

在PostgreSQL中,您可以利用EXISTS

DELETE FROM waitlist w
 WHERE EXISTS (
     SELECT *
       FROM booking
      WHERE customer = w.customer
        AND day = w.day
        AND flight = w.flight
);

SqlFiddle