简体   繁体   中英

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. I want to be able to list all the occupants regardless if they have a record in the p_occupants_insurance table or not.

    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. Is there a better way to accomplish this and can you help me write out the SQL? Sorry, SQL is not my strongest point.

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:

 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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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