完整性检查:这个SQL查询会加入我想要的吗?

时间:2014-10-16 13:38:22

标签: php mysql sql

我最深切地为查询道歉,这是您的遗留代码。我基本上想检查我的代码是否合乎逻辑。

基本上我想要返回"签名"的所有行。表格" claim_id" =" claim_id"关于"索赔"表

由此,我打算检查" signature_id" =每个索赔都为null。如果确实如此,该声明没有相关签名,是否正确?

$SQL="
SELECT
    `claims`.`claim_id`,
    CONCAT(`insurers`.`insurer_name`,IF((`insurers`.`insurer_branch`<>''),CONCAT(' - ',`insurers`.`insurer_branch`),'')) as 'insurer',
    `claims`.`claim_number` as 'claim_number',
    UNIX_TIMESTAMP(claims.overflow_date) as 'overflow_date',
    UNIX_TIMESTAMP(claims.reassigned_date) as 'reassigned_date',
    UNIX_TIMESTAMP(claims.reassigned_from_fr_date) as 'reassigned_from_fr_date',
    UNIX_TIMESTAMP(claims.date_of_first_contact) as 'date_of_first_contact',
    UNIX_TIMESTAMP(`claims`.`date_surveyor_made_contact`) as 'date_surveyor_made_contact',
    UNIX_TIMESTAMP(`claims`.`date_of_inspection`) as 'date_of_inspection',
    UNIX_TIMESTAMP(`claims`.`surveyor_completed`) as 'surveyor_completed',
    UNIX_TIMESTAMP( CASE 1 WHEN (claims.overflow_date>0) THEN claims.overflow_date WHEN (claims.reassigned_date>0) THEN claims.reassigned_date WHEN (claims.reassigned_from_fr_date>0) THEN claims.reassigned_from_fr_date ELSE claims.date_of_first_contact END ) as 'date_of_first_contact_debug',
    `claims`.`date_surveyor_made_contact` as 'date_surveyor_made_contact_debug',
    `claims`.`date_of_inspection` as 'date_of_inspection_debug',
    `claims`.`surveyor_completed` as 'surveyor_completed_debug',
    `claims`.`letter_of_findings`,
    `claims`.`int_settlement_reason`,
    `claims`.`ext_settlement_reason`,
    `claims`.`original_surveyor_id`,
    `claims`.`surveyor_id`,
    `claims`.`broker_name`,
    `insurers`.`insurer_type`,
    `insurers`.`insurer_id`,
    `signatures`.`signature_id`,
    `signatures`.`claim_id`
FROM
    (((`claims` claims INNER JOIN `insurers` insurers ON claims.`insurer_id` = insurers.`insurer_id`)
    INNER JOIN `users` users ON claims.surveyor_id = users.user_id) 
    LEFT JOIN `signatures` signatures on claims.claim_id = signatures.claim_id)
WHERE
    (`claims`.`claim_type` <> ".$conn->qstr(TYPE_DESKTOP).") AND 
    (`claims`.`claim_type` <> ".$conn->qstr(TYPE_AUDIT).") AND  
    (`claims`.`insurer_id` NOT IN ('".implode("','",$arrSURGE)."')) AND 
    (`users`.`status` = ".$conn->qstr(STATUS_LIVE).") AND
    (`claims`.`claim_cancelled_id` = 0) AND
    $where
";

我添加的代码是signatures。选择器和LEFT JOIN 'signatures' signatures on claims.claim_id = signatures.claim_id)行。

claim_id是声明表上的PK,签名表上的signature_id是PK。

谢谢!

1 个答案:

答案 0 :(得分:6)

空检查仅显示带签名的项目。如果将空检查保留在图片之外,则对于不存在签名的项目,您将获得空值。

检查此image