我写了请求,但我担心它可能非常未优化,我会遇到问题。
任务:查找没有债务且上次付款时间超过 2 年的用户。没有任何订单的不必要的用户
我们有两个表:
User tbl (Id (int), LastName (string))
Order tbl (Id (int), UserId (int), IsPaid (bool), DatePaid (Date, not null))
I wrote a Sql request, but I'm afraid I'm down with 20k of users and lots of orders
I have found all those who have no debts and the last payment is for a period of two years.
Now I want to remove them from the general list and so users who need me remain.
It seems bad idea
SELECT u."Id"
FROM "User" AS u
LEFT JOIN
(SELECT *
FROM "Order"
WHERE "UserId" IN
(SELECT "Id"
FROM "User"
WHERE "Id" NOT IN
(SELECT DISTINCT "UserId"
FROM "Order"
WHERE "IsPaid" IS FALSE )
)
AND "DatePaid" > '2016-10-10'
) AS p
ON p."UserId" = u."Id";
最佳答案
我想你只是想要这样的东西:
select o.userid
from orders o
group by o.userid
having sum(case when o.isPaid then 1 else 0 end) = count(*) and -- all are paid
max(o.paymentdate) < current_date - interval '2 year';
请注意,众所周知,日期函数是特定于数据库的。以上使用 ANSI/ISO 标准语法。
如果您想要有关用户的完整信息,则可以使用不同的结构:
select u.*
from users u
where not exists (select 1
from orders o
where o.userid = u.id and not u.ispaid
) and
not exists (select 1
from orders o
where o.userid = u.id and
u.paymentdate > current_date - interval '2 year'
);
关于sql - 如何更好的优化SQL查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52700010/