我有这张桌子。我需要一个查询的帮助,该查询仅显示在9月购买了东西但在10月份没有购买任何东西的客户ID。
customer_id purchase_item qty purchase_date
----------- ------------- --- -------------
5415151 desk organizer 1 9/5/2015
5415151 desk lamp 1 9/5/2015
7884195 pen holder 8 9/7/2015
7894152 flash drive 10 9/19/2015
5987412 universal charger 2 9/28/2015
1264879 desk fan 20 10/7/2015
5415151 filing cabinet 5 10/10/2015
7894152 flash drive 5 10/10/2015
......这就是我想要看到的:
user_id
-------
7894152
5987412
答案 0 :(得分:0)
SELECT T1.customer_id
FROM customer_table T1
LEFT JOIN customer_table T2 ON T1.customer_id = T2.customer_id AND
MONTH(STR_TO_DATE(T1.purchase_date, '%m/%d/%Y')) = 9 AND
MONTH(STR_TO_DATE(T2.purchase_date, '%m/%d/%Y')) = 10
WHERE T2.customer_id IS NULL AND MONTH(STR_TO_DATE(T1.purchase_date, '%m/%d/%Y')) = 9
尝试DEMO SQLFiddle,它会起作用。
希望这有帮助
答案 1 :(得分:0)
尝试这样的查询: -
SELECT customer_id,
group_concat(month((str_to_date(purchase_date,'%m/%d/%y')))) as date1
FROM `customer` group by customer_id
HAVING FIND_IN_SET('9',date1) >0 and FIND_IN_SET('10',date1) =0