如何从下面的sql查询创建codeigniter查询?

时间:2015-07-01 06:59:00

标签: mysql codeigniter model

我有一个像这样的查询工作正常,但我想从中创建一个纯CI查询。我是CI的新手,所以任何人都可以帮我弄清楚如何做到这一点,或者建议我参考网站或链接从哪里可以获得帮助。感谢

SELECT
    user_des,
    IFNULL((sent),0) as sent,
    IFNULL((viewed),0) as viewed,
    CONCAT(IFNULL(fname_usr,user_des),' ',lname_usr,user_des) as fullname,
    IFNULL(sum(duration),0) as totalviewtime,
    IFNULL((totalviews),0) as views,
    IFNULL(sum(sharedcount),0) as shares,
    IFNULL(sum(shared),0) as shared

FROM dem_senddemo
INNER JOIN
(SELECT * FROM dem_demos WHERE id_dem IN (746,943,748) AND customer_dem = '1') as demosfiltered
ON demo_des = id_dem
LEFT JOIN
(
    (
        Select senddemo_wis, sum(duration) as duration, max(datereceived_wis) as datereceived_wis
        FROM
        (
            select senddemo_wis, invited_wis, sum(IFNULL(duration_fea,0) * IFNULL(percentviewed_wis,0)) as duration, max(datereceived_wis) as datereceived_wis
            FROM (sta_views
            LEFT JOIN sta_featureswisitavisits as s ON visit_fvi = id_vis
            LEFT JOIN
                (
                    (SELECT shortvideovid_fea as videoid_fea, shortvideoduration_fea as duration_fea 
                    FROM dem_features
                    where shortvideoduration_fea > 0)
                UNION
                    (SELECT longvideovid_fea as videoid_fea, longvideoduration_fea as duration_fea 
                    FROM dem_features
                    where longvideoduration_fea > 0)
                ) as x
                ON videoid_fea = showedvideo_fvi)
            LEFT JOIN 
            sta_wistia as w ON invited_fvi = invited_wis AND s.showedvideo_fvi = w.mediaid_wis AND wistia_vis = email_wis
            WHERE countvisit_wis <> 'N'
            GROUP BY invited_wis
        ) as durfea
        GROUP BY senddemo_wis

    )  UNION
    (
        SELECT senddemo_wis,sum(IFNULL(mainvideoduration_dem ,0) * IFNULL(percentviewed_wis,0)) as duration, max(datereceived_wis) as datereceived_wis
        FROM sta_wistia as w
        INNER JOIN 
            (select *, customer_dem as cuss FROM dem_demos) as demcorp  ON demo_wis = id_dem AND mainmessagingvideovid_dem = w.mediaid_wis
        LEFT JOIN sta_views ON email_wis = wistia_vis
        WHERE customer_dem = 1 AND senddemo_wis > 0 AND countvisit_wis <> 'N'
        GROUP BY senddemo_wis
    )
) as c ON id_des = senddemo_wis
LEFT JOIN
(
    SELECT user_des as user2_des, count(id_vis) as totalviews  
    FROM sta_views 
    LEFT JOIN dem_invited ON id_invited = invited_vis
    LEFT JOIN dem_senddemo ON id_des = id_senddemo
    WHERE NOT ISNULL(id_senddemo) AND countvisit_wis != 'N' AND createdon_des BETWEEN '2015-05-31 00:00:00' AND '2015-06-29 23:59:59'  AND demo_des IN (746,943,748) 
    GROUP BY user_des
) as z ON user_des = user2_des
LEFT JOIN
(
    SELECT user_des as user3_des, count(id_des) as sent 
    FROM  dem_senddemo 
    LEFT JOIN dem_invited ON id_senddemo = id_des
    WHERE createdon_des BETWEEN '2015-05-31 00:00:00' AND '2015-06-29 23:59:59'  AND demo_des IN (746,943,748)  AND first_invited = 'YES'
    GROUP BY user_des
) as za ON user_des = user3_des
LEFT JOIN
(
    SELECT user_des as user4_des, count(id_vis) as viewed 
    FROM sta_views LEFT JOIN dem_invited ON id_invited = invited_vis
    LEFT JOIN dem_senddemo ON id_des = id_senddemo
    WHERE NOT ISNULL(id_senddemo) AND countvisit_wis != 'N' AND createdon_des BETWEEN '2015-05-31 00:00:00' AND '2015-06-29 23:59:59' AND state = 'VIEWED'
    AND first_invited = 'YES'  AND demo_des IN (746,943,748) 
    GROUP BY user_des
) as zb ON user_des = user4_des
LEFT JOIN
(
    SELECT id_senddemo as iddemdemos2, count(id_senddemo) as shared, allshares as sharedcount
    FROM 
        (
            SELECT id_senddemo, count(id_senddemo) as allshares 
            FROM dem_invited 
            LEFT JOIN dem_senddemo on id_senddemo = id_des 
            WHERE first_invited <> 'YES'  AND demo_des IN (746,943,748)
            GROUP BY id_senddemo
        ) as x
    GROUP BY id_senddemo
) as zc ON iddemdemos2 = id_des
LEFT JOIN 
sec_users ON user_des = id_usr
WHERE customer_dem = 1  AND  createdon_des BETWEEN '2015-05-31 00:00:00' AND '2015-06-29 23:59:59'
GROUP BY user_des

0 个答案:

没有答案