SQL 两个条件合二为一的 LEFT JOIN table
SQL LEFT JOIN with two conditions in one table
我有这样的查询(解释如下):
SELECT
c.customer_name,
c.customer_email,
c.customer_tel,
a.address_1,
a.address_2,
a.postcode
FROM
customer c
LEFT JOIN
address a ON c.customer_id = a.customer_id
WHERE
c.customer_type = 'R'
AND c.customer_id = 25
地址 table 可以保存相同的 customer_id
两次,例如25 但在 customer_type
列中 G
或 R
使其独一无二。
我只想 return 来自地址 table 的地址,其中 customer_id
在 table 中是相同的,并且 customer_type
必须是R
.
现在 return 匹配 customer_id
忽略 customer_type
。
如何确保客户 table 的值始终 return 而地址字段 return 仅当条件 (customer_type
) 匹配时?
SELECT
c.customer_name,
c.customer_email,
c.customer_tel,
a.address_1,
a.address_2,
a.postcode
FROM
customer c
LEFT JOIN address a
ON c.customer_id = a.customer_id AND customer_type ='R'
WHERE
c.customer_type = 'R'
AND c.customer_id = 25
这将适用于 MSSQL 查询。
我有这样的查询(解释如下):
SELECT
c.customer_name,
c.customer_email,
c.customer_tel,
a.address_1,
a.address_2,
a.postcode
FROM
customer c
LEFT JOIN
address a ON c.customer_id = a.customer_id
WHERE
c.customer_type = 'R'
AND c.customer_id = 25
地址 table 可以保存相同的 customer_id
两次,例如25 但在 customer_type
列中 G
或 R
使其独一无二。
我只想 return 来自地址 table 的地址,其中 customer_id
在 table 中是相同的,并且 customer_type
必须是R
.
现在 return 匹配 customer_id
忽略 customer_type
。
如何确保客户 table 的值始终 return 而地址字段 return 仅当条件 (customer_type
) 匹配时?
SELECT
c.customer_name,
c.customer_email,
c.customer_tel,
a.address_1,
a.address_2,
a.postcode
FROM
customer c
LEFT JOIN address a
ON c.customer_id = a.customer_id AND customer_type ='R'
WHERE
c.customer_type = 'R'
AND c.customer_id = 25
这将适用于 MSSQL 查询。