gpt4 book ai didi

sql - 无法在 SQL 查询的 WHERE 子句中使用字段

转载 作者:搜寻专家 更新时间:2023-10-30 22:20:33 24 4
gpt4 key购买 nike

我有这个问题:

select
l.lead_id, l.lead_date_received,
TIMESTAMPDIFF(MINUTE, l.lead_date_received, NOW()) AS minutes,
s.admin_id, a.name, a.email
from
leads l
inner join sales_emails s on l.lead_id = s.lead_id
inner join admin a on a.admin_id = s.admin_id
where
not exists (select 1 from comments c where l.lead_id = c.lead_id)
order by
minutes, l.lead_date_received desc

现在我想在 WHERE 子句中设置条件,例如 minutes > 30。但是,如您所见, minutes 只出现在 SELECT 子句中,而不出现在 FROM 子句或子查询中,这意味着我的条件像 minutes > 30 将不会在 WHERE 子句中被识别。

我该如何解决这个问题?

最佳答案

SELECT l.lead_id, l.lead_date_received, TIMESTAMPDIFF(MINUTE, l.lead_date_received, NOW()) AS minutes, s.admin_id,
a.name, a.email
FROM leads l
INNER JOIN sales_emails s
ON l.lead_id = s.lead_id
INNER JOIN admin a
ON a.admin_id = s.admin_id
WHERE NOT EXISTS (SELECT 1 FROM comments c WHERE l.lead_id = c.lead_id)
AND TIMESTAMPDIFF(MINUTE, l.lead_date_received, NOW()) > 30
ORDER BY minutes, l.lead_date_received DESC

或添加另一个查询:

SELECT *
from
(
SELECT l.lead_id, l.lead_date_received, TIMESTAMPDIFF(MINUTE, l.lead_date_received, NOW()) AS minutes, s.admin_id,
a.name, a.email
FROM leads l
INNER JOIN sales_emails s
ON l.lead_id = s.lead_id
INNER JOIN admin a
ON a.admin_id = s.admin_id
WHERE NOT EXISTS (SELECT 1 FROM comments c WHERE l.lead_id = c.lead_id)
ORDER BY minutes, l.lead_date_received DESC
)
Where minutes > 30

关于sql - 无法在 SQL 查询的 WHERE 子句中使用字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4677626/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com