简体   繁体   English

有关子查询和表联接的SQL帮助

[英]SQL help on subqueries and table joins

Need some sql help...using MYSQL...I've got some legacy code and a situation to resolve...This code below only returns occupants who have a record in the p_occupants_insurance table. 需要一些SQL帮助...使用MYSQL ...我有一些遗留代码和需要解决的情况...下面的代码仅返回在p_occupants_insurance表中有记录的居住者。 I want to be able to list all the occupants regardless if they have a record in the p_occupants_insurance table or not. 我希望能够列出所有占用者,无论他们在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;

I know I can do subqueries like this: 我知道我可以做这样的子查询:

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;

But that is going to lead to a subquery for each column I want out of the p_occupants_insurance table. 但这将导致我要从p_occupants_insurance表中查询的每一列的子查询。 Is there a better way to accomplish this and can you help me write out the SQL? 有没有更好的方法可以做到这一点,您能帮我写出SQL吗? Sorry, SQL is not my strongest point. 抱歉,SQL不是我的强项。

Thanks for the help. 谢谢您的帮助。

Without seeing the schema for each of the tables involved, and relying only on the sample SQL statement, it is difficult to say for sure, but I believe the following will return the result you are looking for: 在没有看到所涉及的每个表的模式的情况下,仅依靠示例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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM