【发布时间】:2009-07-22 18:35:30
【问题描述】:
我有两张桌子,一张叫customer,一张叫customer_attributes。
这个想法是客户表包含核心客户数据,并且可以自定义应用程序以支持其他属性,具体取决于它的使用方式。
customer_attributes 有以下 3 列:
customerID
key1
value1
我是否可以检索完整的行,如果指定了任何附加属性,如果没有,则默认为 NULL?我正在使用以下查询,但它仅在 customer_attributes 表中存在这两个属性时才有效。
SELECT `customer`.*, `ca1`.`value1` AS `wedding_date`, `ca2`.`value1` AS `test`
FROM `customer`
LEFT JOIN `customer_attributes` AS `ca1` ON customer.customerID = ca1.customerID
LEFT JOIN `customer_attributes` AS `ca2` ON customer.customerID = ca2.customerID
WHERE (customer.customerID = '58029')
AND (ca1.key1 = 'wedding_date')
AND (ca2.key1 = 'test')
在这种情况下,我感兴趣的两个属性称为“wedding_date”和“test”
【问题讨论】: