gpt4 book ai didi

mysql - 有关子查询和表连接的 SQL 帮助

转载 作者:行者123 更新时间:2023-11-29 06:58:07 25 4
gpt4 key购买 nike

需要一些 sql 帮助...使用 MYSQL...我有一些遗留代码和需要解决的情况...下面的代码仅返回在 p_ocpants_insurance 表中有记录的占用者。我希望能够列出所有居住者,无论他们是否在 p_occupants_insurance 表中都有记录。

    SELECT a.occupants_insurance_id, a.occupant_id, a.policy_nbr, a.policy_type, a.coverage_amount_curr,

CASE
WHEN a.effective_date = '0000-00-00' THEN NULL
ELSE a.effective_date
END as effective_date,

CASE
WHEN a.expiration_date = '0000-00-00' THEN NULL
ELSE a.expiration_date
END as expiration_date,

a.insurance_company, a.custom1_label, a.custom2_label, a.custom3_label, a.custom1, a.custom2, a.custom3, c.name as prop_name, (SELECT x.name FROM portfolio_hierarchy x WHERE x.leaf_node_portf_id = d.portfolio_id ) as p_name, b.name as occupant_name, b.primary_contact, b.phone
FROM p_occupants_insurance a, p_occupants b, properties c, portfolio d
WHERE a.occupant_id = b.occupant_id
AND b.property_id = c.properties_id
AND c.portfolio_id = d.portfolio_id
AND d.account_id = 1
AND b.archived = 0
AND b.trashbin = 0
ORDER BY d.p_name ASC, prop_name ASC, occupant_name ASC, insurance_company ASC, policy_nbr ASC;

我知道我可以像这样进行子查询:

SELECT b.name as occupant_name, b.primary_contact, b.phone,

(SELECT a.occupants_insurance_id FROM p_occupants_insurance a WHERE a.occupants_id = b.occupants_id) as occupants_insurance_id


FROM p_occupants b, properties c, portfolio d
WHERE a.occupant_id = b.occupant_id
AND b.property_id = c.properties_id
AND c.portfolio_id = d.portfolio_id
AND d.account_id = 1
AND b.archived = 0
AND b.trashbin = 0
ORDER BY d.p_name ASC, prop_name ASC, occupant_name ASC, insurance_company ASC, policy_nbr ASC;

但这将导致对我想要从 p_occupants_insurance 表中取出的每一列进行子查询。有没有更好的方法来完成这个任务,你能帮我写出 SQL 吗?抱歉,SQL 不是我的强项。

感谢您的帮助。

最佳答案

如果没有看到所涉及的每个表的架构,并且仅依靠示例 SQL 语句,很难确定,但我相信以下内容将返回您正在寻找的结果:

 SELECT 
a.`occupants_insurance_id`,
a.`occupant_id`,
a.`policy_nbr`,
a.`policy_type`,
a.`coverage_amount_curr`,
IF(a.`effective_date` = '0000-00-00',NULL,a.`effective_date`) as a.`effective_date`,
IF(a.`expiration_date` = '0000-00-00',NULL,a.`expiration_date`) as a.`expiration_date`,
a.`insurance_company`,
a.`custom1_label`,
a.`custom2_label`,
a.`custom3_label`,
a.`custom1`,
a.`custom2`,
a.`custom3`,
c.`name` as `prop_name`,
(SELECT x.name FROM portfolio_hierarchy x WHERE x.leaf_node_portf_id = d.portfolio_id ) as `p_name`,
b.`name` as `occupant_name`,
b.`primary_contact`,
b.`phone`
FROM `p_occupants` b
LEFT JOIN `p_occupants_insurance` a
ON a.`occupant_id` = b.`occupant_id`
JOIN `properties` c
ON b.`property_id` = c.`properties_id`
JOIN `portfolio` d
ON c.`portfolio_id` = d.`portfolio_id`
WHERE d.account_id = 1
AND b.archived = 0
AND b.trashbin = 0
ORDER BY d.p_name ASC,
`prop_name` ASC,
`occupant_name` ASC,
`insurance_company` ASC,
`policy_nbr` ASC;

关于mysql - 有关子查询和表连接的 SQL 帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44769794/

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