我有articles
和discounts
表。 Articles
可能有家庭和子家庭(或不是,可能是空白的)。
文章:
id | name | price | family | subfamily
1 | art1 | 5 | F01 | A02
2 | art2 | 5.5 | F02 |
折扣
id | customer_id | family | subfamily | discount
1 | ABC123 | F01 | A02 | 40%
2 | CBD321 | F02 | | 30%
我需要检索基于以下内容的折扣的文章:customer_id
(将订购的客户)系列和子系列。文章可以根据他的家庭(子家庭是空白的)或他的家庭和亚家庭有折扣,但两者都需要与桌上的顾客ID相关联。如果一篇文章没有匹配任何内容,那么该客户的折扣将为空。
我该怎么做?我试过这个,但只能像INNER JOIN
SELECT
a.*,
d.discount
FROM articles a
LEFT JOIN discounts AS d
ON a.family = d.family
AND a.subfamily = d.subfamily
WHERE d.customer_id = 'ABC123'
答案 0 :(得分:2)
将折扣条件从SELECT a.*, d.discount
FROM articles a
LEFT JOIN discounts AS d ON a.family = d.family AND a.subfamily = d.subfamily
AND d.customer_id = 'ABC123'
移至GET
以获得真实list
结果:
{
'response':[{}, {}],
'status': 200
}