我怎样才能继续处理一条记录,即使它不存在于 table B 中?

How can I continue processing a record, even if it doesn't exist in table B?

可能是一个愚蠢的问题,但会提供一个基本的表示示例。我有 2 tables 和一个带条件语句的查询。 Table A 是主要帐户(如果您愿意的话),如果帐户存在于 Table B 中,那将是一个加号。如果不是,则 return 为空。目前,只有return记录A和B中是否存在账户。示例:

SELECT
A.id_nbr AS ID,
A.contact_type AS contype,
A.last_update AS date,
B.card_type as card_type
FROM id_info A,
     billing_info B
WHERE (A.contact_type = 'AAA' OR A.contact_type = 'BBB' OR A.contact_type = 'CCC')
AND A.id_nbr = B.id_nbr;

如果记录存在,很好:

ID          contype     date         card_type
111111111   AAA         2020-02-21   MS

如果没有,也很好:

ID          contype     date         card_type
222222222   AAA         2020-02-21   null

基本上,如果 table B 中不存在,我不想否定整个记录。 任何建议表示赞赏。非常感谢!

使用左连接

SELECT
A.id_nbr AS ID,
A.contact_type AS contype,
A.last_update AS date,
B.card_type as card_type
FROM id_info A
LEFT JOIN billing_info B ON  (A.contact_type = 'AAA' OR A.contact_type = 'BBB' OR A.contact_type = 'CCC')
    AND A.id_nbr = B.id_nbr;

您需要一个外连接,为此您可以使用显式连接语法和左连接。

根据两个表中列 id_nbr 的相等性,使用从 id_infobilling_infoLEFT 连接就是这种情况,但其他条件如果您只想要这 3 种类型的结果而不是 OR,则必须在 WHERE 子句中设置,使用 IN 运算符更简单:

SELECT
  A.id_nbr AS ID,
  A.contact_type AS contype,
  A.last_update AS date,
  B.card_type as card_type
FROM id_info A LEFT JOIN billing_info B
ON AND A.id_nbr = B.id_nbr
WHERE A.contact_type IN ('AAA', 'BBB', 'CCC')