gpt4 book ai didi

mysql - 尽管有 Null/None 和重复项,仍返回一行

转载 作者:行者123 更新时间:2023-11-29 13:48:31 25 4
gpt4 key购买 nike

我有一个查询接近我想要的但不完全是:

SELECT DISTINCT p.id, 
Ifnull(p.last_name, '--') last_name,
Ifnull(p.first_name, '--') first_name,
Ifnull(p.city, '--') city,
Ifnull(p.state, '--') state,
Ifnull(e.full_name, '--') full_name,
Ifnull(o.current_step, '--') current_step,
Ifnull(o.current_step_date, '--') current_step_date
FROM prospect AS p
JOIN opportunity AS o
ON o.prospect_id = p.id
JOIN employee AS e
ON p.id_ofproducer = e.id
WHERE p.id = 1234

我希望能从 P 处取回该行

注意:如果有多个 o 或 e 记录,则使用 MAX ID,如果没有,则使用“--”

最佳答案

您需要一个左连接来处理没有记录的情况。并且,您需要找到 oe 记录的最大值。实际上,您不应该对 e 记录有问题,因为您正在加入 id。以下是制定查询的一种方法:

SELECT DISTINCT p.id, 
Ifnull(p.last_name, '--') last_name,
Ifnull(p.first_name, '--') first_name,
Ifnull(p.city, '--') city,
Ifnull(p.state, '--') state,
Ifnull(e.full_name, '--') full_name,
Ifnull(o.current_step, '--') current_step,
Ifnull(o.current_step_date, '--') current_step_date
FROM prospect AS p
left JOIN opportunity AS o
ON o.prospect_id = p.id and
o.id = (select id from opportunity o2 where o2.prospect_id = p.id order by id desc limit 1)
left JOIN employee AS e
ON p.id_ofproducer = e.id
WHERE p.id = 1234

关于mysql - 尽管有 Null/None 和重复项,仍返回一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17074517/

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