查找重新订购相同购买商品的客户,MySql

时间:2018-12-17 16:44:03

标签: mysql group-by

我有3个表:users,orders和order_prices,后者包含每个订单的分析,我需要查找在特定月份重新订购的客户(两个表都有user_id,order_id,order_date,good_id(仅order_prices)等),我的脚本如下:

select o.system_id, o.user_id, o.date, o.id 

from orders o

where o.date >= '2018-11-01' and o.date <= '2018-11-30' -- in the date range
AND o.user_id in 
            (   -- has made the same order (content)
                select op.user_id 

                from order_prices op join users u on u.id = op.user_id

                where op.good_id in 
                            (                                                      
                                select good_id 

                                from order_prices 

                                where user_id = o.user_id

                            )

                    And u.number_of_orders > 0 -- has ordered before

            ) 


group by o.user_id

好吧,我不确定这是否在逻辑上也行得通,我想我还需要找到那些用户,他们先前的顺序与他们的最后顺序在内容上是相同的,上述脚本没有考虑到...。建议多于欢迎!

0 个答案:

没有答案