Postgres - 基于另一个表的SQL查询删除。

时间:2017-09-28 12:42:55

标签: mysql sql-server postgresql

我无法弄清楚如何将此查询转换为DELETE-statment。

从购物车中选择DISTINCT carts.id,shows.date

ticket.cart = carts.id

上的

INNER JOIN门票 actseats.ticket = tickets.id

INNER JOIN actseats

INNER JOIN showacts on actseats.showact = showacts.id

INNER JOIN显示在shows.id = showacts.show

WHERE Shows.date> =' 2017-05-05';

我从此查询中获取的购物车是我要删除的商品..在删除规则中。

2 个答案:

答案 0 :(得分:-1)

WITH val AS (
SELECT DISTINCT carts.id cartid, shows.date from carts

INNER JOIN tickets on tickets.cart = carts.id

INNER JOIN actseats on actseats.ticket = tickets.id

INNER JOIN showacts on actseats.showact = showacts.id

INNER JOIN shows on shows.id = showacts.show

WHERE Shows.date >= '2017-05-05' )
DELETE FROM carts WHERE carts.id IN (SELECT cartid FROM val)

答案 1 :(得分:-1)

你可以尝试

delete from carts where id in (SELECT DISTINCT carts.id from carts

INNER JOIN tickets on tickets.cart = carts.id

INNER JOIN actseats on actseats.ticket = tickets.id

INNER JOIN showacts on actseats.showact = showacts.id

INNER JOIN shows on shows.id = showacts.show

WHERE Shows.date >= '2017-05-05');

我希望这对你有所帮助。