0

我有以下查询语句:

$query_string = '
    SELECT customerID, lastName, firstName, companyName, email, citizenship, primaryLanguage
    FROM customers
    JOIN citizenships USING(citizenshipID)
    JOIN languages USING(languageID)
    JOIN paymentMethods USING(customerID)
    WHERE customerID = "1"
';

目前,客户、公民和语言表每个都包含行并正确连接。我的查询结果为客户 #1 返回 1 行。

paymentMethods 表此时不包含任何行。当我将 paymentMethods 的连接语法添加到查询字符串时,我的查询结果为客户 #1 返回 0 行。

我想加入 paymentMethods 并且仅在 paymentMethods 表存在时返回一行,否则不会导致不返回任何客户行。

我该如何调整我的 JOIN 语法来实现这一点?

谢谢你。

4

2 回答 2

1

将其替换为LEFT JOIN

SELECT  customerID, lastName, firstName, companyName, email, citizenship, primaryLanguage
FROM    customers
JOIN    citizenships
USING   (citizenshipID)
JOIN    languages
USING   (languageID)
LEFT JOIN
        paymentMethods
USING   (customerID)
WHERE   customerID = "1"
于 2013-01-17T17:41:04.867 回答
1
$query_string = '
   SELECT customerID, lastName, firstName, companyName, email, citizenship,   primaryLanguage
   FROM customers
   JOIN citizenships USING(citizenshipID)
   JOIN languages USING(primaryLanguageID)
   LEFT JOIN paymentMethids USING(customerID)
   WHERE customerID = "1"
';
于 2013-01-17T17:41:09.120 回答