史上最長mysql

每小時抓取淘寶商品數據, 統計淘寶 日,周,月的數據

select  

        ifNull(t1.count,0) as count,

        ifNull(t1.tb_day30,0) as tb_day30,

        ifNull(t1.tb_day7,0) as tb_day7,

        ifNull(t1.tb_day1,0) as tb_day1,

                ifNull(t1.tb_day0,0) as tb_day0,

        ifNull(t1.tb_dayAll,0) as tb_dayAll,

                t1.test,

        ifNull(t1.taobao_inventory,0) as taobao_inventory,

        t1.taobao_id,



        wheel_goods.name,wheel_goods.taobao_img,wheel_goods.good_id,wheel_goods.approve_status,

        ifNull(wheel_day30,0) as wheel_day30,

        ifNull(wheel_day7,0) as wheel_day7,

        ifNull(wheel_day1,0) as wheel_day1,

        ifNull(wheel_dayAll,0) as wheel_dayAll,

        ifNull(tb_dayAll,0) + ifNull(wheel_dayAll,0) as dayAll ,

        wheel_inventory

        from 

        (

        select 

        sum(if( t_eggs.egg_type=0 and TO_DAYS(FROM_UNIXTIME(t_eggs.time/1000)) > TO_DAYS(now())-30 and in_collect=0,1,0)) as wheel_day30,

        sum(if( t_eggs.egg_type=0 and TO_DAYS(FROM_UNIXTIME(t_eggs.time/1000)) > TO_DAYS(now())-7 and in_collect=0,1,0)) as wheel_day7,

        sum(if( t_eggs.egg_type=0 and TO_DAYS(FROM_UNIXTIME(t_eggs.time/1000)) > TO_DAYS(now())-1 and in_collect=0,1,0)) as wheel_day1,

        sum(if( t_eggs.egg_type=0 and in_collect=0,1,0)) as wheel_dayAll,

        taobao_id,

        t_goods.taobao_img,

        t_goods.name,

        t_goods.approve_status,

            

        t_goods.good_id,

        t_egg_good_rel.egg_good_inventory,

        t_goods.inventory,

            if( t_egg_good_rel.egg_good_inventory>0,t_egg_good_rel.egg_good_inventory,t_goods.inventory) as wheel_inventory




        from 

        t_goods

        LEFT JOIN 

        (select t_egg_play_records.egg_id,good_id,t_egg_play_records.time,egg_type,in_collect from 

        t_egg_play_records,t_eggs where t_eggs.egg_id = t_egg_play_records.egg_id ) as t_eggs  on t_goods.good_id = t_eggs.good_id 

        left join t_egg_good_rel on t_egg_good_rel.egg_id=t_eggs.egg_id and t_egg_good_rel.good_id = t_eggs.good_id

        GROUP BY t_goods.good_id



        ) as wheel_goods 

        LEFT JOIN 

        (

        SELECT  

        count( to_days(from_unixtime(t4.create_time/1000))>to_days(now())-30 ) as count,

            

            if( 

                count( to_days(from_unixtime(t4.create_time/1000))>to_days(now())-30 ) = 1 and to_days(from_unixtime(t4.create_time/1000))>to_days(now())-30 ,

                max(taobao_sold_quantity),

                ifNull((max( if(to_days(from_unixtime(t4.create_time/1000))>to_days(now())-30 ,t4.max_taobao_sold_quantity,null ))-min( if(to_days(from_unixtime(t4.create_time/1000))>to_days(now())-30 ,t4.min_taobao_sold_quantity,null ))),0)

        ) as tb_day30,

        if( 

                count( to_days(from_unixtime(t4.create_time/1000))>to_days(now())-7 ) = 1 and to_days(from_unixtime(t4.create_time/1000))>to_days(now())-7 ,

                max(taobao_sold_quantity),

                ifNull((max( if(to_days(from_unixtime(t4.create_time/1000))>to_days(now())-7 ,t4.max_taobao_sold_quantity,null ))-min( if(to_days(from_unixtime(t4.create_time/1000))>to_days(now())-7 ,t4.min_taobao_sold_quantity,null ))),0)

        ) as tb_day7,

        if( 

                count( to_days(from_unixtime(t4.create_time/1000))=to_days(now())-1 ) = 1 and to_days(from_unixtime(t4.create_time/1000))=to_days(now())-1 ,

                max(taobao_sold_quantity),

                ifNull((max( if(to_days(from_unixtime(t4.create_time/1000))=to_days(now())-1 ,t4.max_taobao_sold_quantity,null ))-min( if(to_days(from_unixtime(t4.create_time/1000))=to_days(now())-1 ,t4.min_taobao_sold_quantity,null ))),0)

        ) as tb_day1,

    if( 

                count( to_days(from_unixtime(t4.create_time/1000))=to_days(now()) ) = 0 and to_days(from_unixtime(t4.create_time/1000))=to_days(now()) ,

                max(taobao_sold_quantity),

                ifNull((max( if(to_days(from_unixtime(t4.create_time/1000))=to_days(now())-0 ,t4.max_taobao_sold_quantity,null ))-min( if(to_days(from_unixtime(t4.create_time/1000))=to_days(now())-0 ,t4.min_taobao_sold_quantity,null ))),0)

        ) as tb_day0,

            max( t4.taobao_sold_quantity) as test,

        max(taobao_sold_quantity) as tb_dayAll,

        t1.taobao_inventory,

        t4.taobao_id

        FROM 

        (select t_taobao_good_change.*,max(taobao_sold_quantity) as max_taobao_sold_quantity ,min(taobao_sold_quantity) as min_taobao_sold_quantity 

        from t_taobao_good_change GROUP BY taobao_id, to_days(FROM_UNIXTIME(create_time/1000))) as  t4 

                left join 

                (select taobao_id ,taobao_inventory 

                from t_taobao_good_change t3 

                where  (t3.create_time,t3.taobao_id )  in (SELECT max(t2.create_time),t2.taobao_id FROM t_taobao_good_change t2  group by taobao_id) ) as t1 on t4.taobao_id = t1.taobao_id

                group by t4.taobao_id

        

            ) as t1 

        

        on wheel_goods.taobao_id =  t1.taobao_id

    

    order by approve_status desc ,tb_day7 desc ,good_id

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章