我有五个表,它们的关系在图片中列出。我想编写一个查询来显示在2013年7月15日至2013年7月31日期间购买了Foresters Best品牌面漆的所有客户的名字,姓氏,街道,城市,州和邮编。客户购买了不止一种这样的产品,在输出中仅显示一次客户的信息。按状态,姓氏和名字对输出进行排序。
我可以只使用一个条件进行查询,但是对于这种多个(也许是缩进的)条件,我完全陷于困境。我可以这样分析结构:
IN LGBRAND TABLE: Brand_ID = 23 ~ Brand_Name = "Foresters Best"
inv_date from lginvoice where inv_date between "2013-7-15" and "2013-7-31"
prod_category from lgproduct = "Top Coat"
谢谢!
答案 0 :(得分:2)
很难确定没有样本数据,但是类似的事情应该起作用:
SELECT DISTINCT c.*
FROM LGCUSTOMER c
JOIN LGINVOICE i ON i.Cust_Code = c.Cust_Code
JOIN LGLINE l ON l.Inv_Num = i.Inv_Num
JOIN LGPRODUCT p ON p.Prod_SKU = l.Prod_SKU
JOIN LGBRAND b ON b.Brand_ID = p.Brand_ID
WHERE b.Brand_Name = 'Foresters Best' AND
p.Prod_Category = 'Top Coat' AND
i.Inv_Date BETWEEN '2013-07-15' AND '2013-07-31'
ORDER BY c.Cust_State, c.Cust_Lname, c.Cust_Fname
答案 1 :(得分:0)
select distinct(Cust_Fname),distinct(Cust_Lname),distinct(Cust_Street),distinct(Cust_City),distinct(Cust_State),distinct(Cust_ZIP) from lgcustomer as cust
join (select inv_num,cust_code from lginvoice where CAST(inv_date AS DATE) between '2013-07-15' and '2013-07-31') inv on inv.cust_code = cust.cust_code
join (select inv_num, prod_sku from lgline) ll on ll.inv_num = inv.inv_num
join (select prod_sku,prod_descipt, brand_id from lgproduct where prod_descipt like "%Top Coat%") lgp on lgp.prod_sku = ll.prod_sku
join (select brand_id, brand_name from lgbrand where brand_name like "%Foresters Best%") lb on lb.brand_id = lgp.brand_id
order by 5,2,1 desc