题目链接:点击打开链接
题目大意:略。
解题思路:注意如何将 COUNT(带条件判断) 转化为 SUM() 小技巧。
AC 代码
--解决方案(1) SELECTuser_idbuyer_id, join_date, IFNULL(cnt, 0) orders_in_2019FROMUsersuLEFTJOIN(SELECTbuyer_id, COUNT(1) cnt, order_dateFROMOrdersWHEREorder_datebetween'2019-01-01'AND'2019-12-31'GROUPBYbuyer_id) RSONu.user_id=RS.buyer_id--解决方案(2) SELECTuser_idbuyer_id, join_date, IFNULL(cnt, 0) orders_in_2019FROMUsersuLEFTJOIN(SELECTbuyer_id, SUM(IF(order_datebetween'2019-01-01'AND'2019-12-31', 1, 0)) cntFROMOrdersGROUPBYbuyer_id) RSONu.user_id=RS.buyer_id