Py学习  »  DATABASE

在MySQL中选择具有多条件要求的行

Steve • 7 年前 • 2360 次点击  

我有五张桌子,它们的关系都列在图片中。我想写一个查询来显示在2013年7月15日至2013年7月31日期间购买了Foresters Best品牌大衣的任何客户的名字、姓氏、街道、城市、州和邮政编码。如果客户购买了多个这样的产品,那么在输出中只显示一次客户的信息。按状态、姓氏和名字对输出进行排序。

我可以只使用一个条件进行查询,但是对于这个倍数(可能是缩进的?)情况,我完全卡住了。我可以这样分析结构:

in lgbrand table:brand_id=23~ brand_name=“foresters best”
从LGInvoice开始的投资日期,其中投资日期介于“2013-7-15”和“2013-7-31”之间
lgproduct=“面漆”中的产品类别
< /代码> 

谢谢!

enter image description here

我可以只使用一个条件进行查询,但是对于这个倍数(可能是缩进的?)情况,我完全卡住了。我可以这样分析结构:

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"

谢谢您!

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/30354
文章 [ 2 ]  |  最新文章 7 年前
Sathvik Reddy
Reply   •   1 楼
Sathvik Reddy    7 年前
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
Nick Loay Oraby
Reply   •   2 楼
Nick Loay Oraby    7 年前

没有样本数据是很难确定的,但是这样的事情应该是有效的:

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