如何结合两个表和日期明智地获取数据任何想法给我和

时间:2018-08-21 18:17:39

标签: mysqli phpmyadmin

DELIMITER $$   创建程序masmagnu(输入pid文本,cyear日期,lessmsdt日期,lessmedt日期,thsdt日期,thedt日期)     开始         如果存在(选择*从vera_vasulat vv中,vv.p_id = pid和vv.pavati_date> = lessmsdt和vv.pavati_date <= thedt)然后

        IF EXISTS(SELECT * FROM vera_vasulat vv WHERE vv.p_id=pid AND vv.pavati_date>=thsdt AND vv.pavati_date<=thedt) THEN

            IF EXISTS(SELECT * FROM vera_vasulat vv WHERE vv.p_id=pid AND vv.pavati_date>=lessmsdt AND vv.pavati_date<=lessmedt) THEN

                 SELECT 
                    (a.vera_id) as vera_id,
                    (a.vera_name)as vera_name,
                    (a.pachalu_magnu-b.pachalu_vasulat) as pbm,
                    (a.chalu_magnu-b.chalu_vasulat) as cbm,
                    ((a.pachalu_magnu-b.pachalu_vasulat)+(a.chalu_magnu-b.chalu_vasulat)) as pbcbm,
                    (a.gy-b.p1) as pbg,
                    (a.cy-b.c1) as cbg,
                    ((a.gy-b.p1)+(a.cy-b.c1)) as pbcbg,
                    (a.pachalu_magnu-b.pbc) as pbc,
                    (a.chalu_magnu-b.cbc) as cbc,
                    ((a.pachalu_magnu-b.pbc)+(a.chalu_magnu-b.cbc)) as pbccbc,
                    ((a.gy-b.p1)+(a.pachalu_magnu-b.pbc)) as kvp,
                    ((a.cy-b.c1)+(a.chalu_magnu-b.cbc)) as kvc,
                    (((a.gy-b.p1)+(a.pachalu_magnu-b.pbc))+((a.cy-b.c1)+(a.chalu_magnu-b.cbc))) as kvcp,
                    ((a.pachalu_magnu-b.pachalu_vasulat)-(a.gy-b.p1)-(a.pachalu_magnu-b.pbc)) as kbp,
                    ((a.chalu_magnu-b.chalu_vasulat)-(a.cy-b.c1)-(a.chalu_magnu-b.cbc)) as kbc,
                    (((a.pachalu_magnu-b.pachalu_vasulat)-(a.gy-b.p1)-(a.pachalu_magnu-b.pbc))+((a.chalu_magnu-b.chalu_vasulat)-(a.cy-b.c1)-(a.chalu_magnu-b.cbc))) as kbcp FROM
                        (SELECT 
                            v.vera_id,
                            v.vera_name,
                            (SUM(avk.chalu_year)) chalu_magnu,
                            (SUM(avk.gaya_year)) pachalu_magnu,
                            (SUM(aa.chalu_year)) cy,
                            (SUM(aa.gaya_year)) gy 
                                FROM akarni_vera avk 
                                INNER JOIN vera_vigat v 
                                    ON avk.vera_id=v.vera_id 
                                INNER JOIN akarni_vera aa 
                                    ON aa.vera_id=v.vera_id 
                                WHERE v.p_id=pid AND
                                    avk.p_id=pid AND
                                    avk.akarni_date>=cyear AND 
                                    avk.akarni_date<=thedt AND
                                    aa.p_id=pid AND 
                                    aa.akarni_date>=cyear AND 
                                    aa.akarni_date<=lessmedt 
                                GROUP BY avk.vera_id) a
                JOIN 
                    (select 
                        vv.vera_id,
                        vv.vera_name,
                        SUM(vvl.c1) chalu_vasulat,
                        SUM(vvl.p1) pachalu_vasulat,
                        SUM(vl.c1) c1,
                        SUM(vl.p1) p1,
                        SUM(lv.p1) pbc,
                        SUM(lv.c1) cbc
                            FROM vera_vasulat_list vvl 
                            INNER JOIN vera_vigat vv 
                                ON vvl.vera_id=vv.vera_id 
                            INNER JOIN vera_vasulat_list vl 
                                ON vl.vera_id=vv.vera_id 
                            INNER JOIN vera_vasulat_list lv 
                                ON lv.vera_id=vv.vera_id 
                            WHERE 
                                vv.p_id=pid AND 
                                vvl.p_id=pid AND 
                                vvl.pavati_date>=cyear AND 
                                vvl.pavati_date<=thedt AND 
                                vl.p_id=pid AND 
                                vl.pavati_date>=lessmsdt AND
                                vl.pavati_date<=lessmedt AND 
                                lv.p_id=pid AND 
                                lv.pavati_date>=thsdt AND
                                lv.pavati_date<=thedt 
                            GROUP BY vvl.vera_id) b
                    USING(vera_id);

            ELSE

                SELECT 
                    (a.vera_id) as vera_id,
                    (a.vera_name)as vera_name,
                    (a.pachalu_magnu-b.pachalu_vasulat) as pbm,
                    (a.chalu_magnu-b.chalu_vasulat) as cbm,
                    ((a.pachalu_magnu-b.pachalu_vasulat)+(a.chalu_magnu-b.chalu_vasulat)) as pbcbm,
                    (a.pachalu_magnu-b.pbc) as pbc,
                    (a.chalu_magnu-b.cbc) as cbc,
                    ((b.pachalu_vasulat-b.pbc)+(a.chalu_magnu-b.cbc)) as pbccbc,
                    (a.pachalu_magnu-b.pbc) as kvp,
                    (a.chalu_magnu-b.cbc) as kvc,
                    ((a.pachalu_magnu-b.pbc)+(a.chalu_magnu-b.cbc)) as kvcp,
                    ((a.pachalu_magnu-b.pachalu_vasulat)-(a.pachalu_magnu-b.pbc)) as kbp,
                    ((a.chalu_magnu-b.chalu_vasulat)-(a.chalu_magnu-b.cbc)) as kbc,
                    (((a.pachalu_magnu-b.pachalu_vasulat)-(a.pachalu_magnu-b.pbc))+((a.chalu_magnu-b.chalu_vasulat)-(a.chalu_magnu-b.cbc))) as kbcp FROM
                        (SELECT 
                            v.vera_id,
                            v.vera_name,
                            (SUM(avk.chalu_year)) chalu_magnu,
                            (SUM(avk.gaya_year)) pachalu_magnu,
                            (SUM(aa.chalu_year)) cy,
                            (SUM(aa.gaya_year)) gy 
                                FROM akarni_vera avk 
                                INNER JOIN vera_vigat v 
                                    ON avk.vera_id=v.vera_id 
                                INNER JOIN akarni_vera aa 
                                    ON aa.vera_id=v.vera_id 
                                WHERE v.p_id=pid AND
                                    avk.p_id=pid AND
                                    avk.akarni_date>=cyear AND 
                                    avk.akarni_date<=thedt AND
                                    aa.p_id=pid AND 
                                    aa.akarni_date>=cyear AND 
                                    aa.akarni_date<=lessmedt 
                            GROUP BY avk.vera_id) a
                JOIN 
                    (SELECT 
                        vv.vera_id,
                        vv.vera_name,
                        SUM(vvl.c1) chalu_vasulat,
                        SUM(vvl.p1) pachalu_vasulat,
                        SUM(lv.p1) pbc,
                        SUM(lv.c1) cbc,
                        SUM(kv.c1) kvc,
                        SUM(kv.p1) kvp 
                            FROM vera_vasulat_list vvl 
                            INNER JOIN vera_vigat vv 
                                ON vvl.vera_id=vv.vera_id 
                            INNER JOIN vera_vasulat_list kv 
                                ON kv.vera_id=vv.vera_id 
                            INNER JOIN vera_vasulat_list lv 
                                ON lv.vera_id=vv.vera_id 
                            WHERE 
                                vv.p_id=pid AND 
                                vvl.p_id=pid AND 
                                vvl.pavati_date>=cyear AND 
                                vvl.pavati_date<=thedt AND 
                                kv.p_id=pid AND 
                                kv.pavati_date>=cyear AND 
                                kv.pavati_date<=thedt AND 
                                lv.p_id=pid AND 
                                lv.pavati_date>=thsdt AND
                                lv.pavati_date<=thedt
                            GROUP BY vvl.vera_id) b
                    USING(vera_id);

            END IF;

        ELSEIF EXISTS(SELECT * FROM vera_vasulat vv WHERE vv.p_id=pid AND vv.pavati_date>=lessmsdt AND vv.pavati_date<=lessmedt) THEN

            SELECT 
                (a.vera_id) as vera_id,
                (a.vera_name)as vera_name,
                (a.pachalu_magnu-b.pachalu_vasulat) as pbm,
                (a.chalu_magnu-b.chalu_vasulat) as cbm,
                ((a.pachalu_magnu-b.pachalu_vasulat)+(a.chalu_magnu-b.chalu_vasulat)) as pbcbm,
                (a.gy-b.p1) as pbg,
                (a.cy-b.c1) as cbg,
                ((a.gy-b.p1)+(a.cy-b.c1)) as pbcbg,
                (a.gy-b.p1) as kvp,
                (a.cy-b.c1) as kvc,
                ((a.gy-b.p1)+(a.cy-b.c1)) as kvcp,
                ((a.pachalu_magnu-b.pachalu_vasulat)-(a.gy-b.p1)) as kbp,
                ((a.chalu_magnu-b.chalu_vasulat)-(a.cy-b.c1)) as kbc,
                (((a.pachalu_magnu-b.pachalu_vasulat)-(a.gy-b.p1))+((a.chalu_magnu-b.chalu_vasulat)-(a.cy-b.c1))) as kbcp FROM
                    (SELECT 
                        v.vera_id,
                        v.vera_name,
                        (SUM(avk.chalu_year)) chalu_magnu,
                        (SUM(avk.gaya_year)) pachalu_magnu,
                        (SUM(aa.chalu_year)) cy,
                        (SUM(aa.gaya_year)) gy 
                            FROM akarni_vera avk 
                            INNER JOIN vera_vigat v 
                                ON avk.vera_id=v.vera_id 
                            INNER JOIN akarni_vera aa 
                                ON aa.vera_id=v.vera_id 
                            WHERE v.p_id=pid AND
                                avk.p_id=pid AND
                                avk.akarni_date>=cyear AND 
                                avk.akarni_date<=thedt AND
                                aa.p_id=pid AND 
                                aa.akarni_date>=cyear AND 
                                aa.akarni_date<=lessmedt 
                            GROUP BY avk.vera_id,aa.vera_id) a
            JOIN 
                (SELECT 
                    vv.vera_id,
                    vv.vera_name,
                    SUM(vvl.c1) chalu_vasulat,
                    SUM(vvl.p1) pachalu_vasulat,
                    SUM(vl.c1) c1,
                    SUM(vl.p1) p1
                        FROM vera_vasulat_list vvl 
                        INNER JOIN vera_vigat vv 
                            ON vvl.vera_id=vv.vera_id 
                        INNER JOIN vera_vasulat_list vl 
                            ON vl.vera_id=vv.vera_id 
                        WHERE 
                            vv.p_id=pid AND 
                            vvl.p_id=pid AND 
                            vvl.pavati_date>=cyear AND 
                            vvl.pavati_date<=thedt AND 
                            vl.p_id=pid AND 
                            vl.pavati_date>=cyear AND
                            vl.pavati_date<=lessmedt
                        GROUP BY vvl.vera_id,vl.vera_id) b
                USING(vera_id);

        END IF;

    ELSE

        SELECT 
            (v.vera_id) as vera_id,
            (v.vera_name) as vera_name,
            SUM(aa.gaya_year) as pbm,
            SUM(aa.chalu_year) as cbm,
            (SUM(aa.gaya_year)+SUM(aa.chalu_year)) as pbcbm 
                FROM akarni_vera aa 
                INNER JOIN vera_vigat v 
                    ON aa.vera_id=v.vera_id 
                WHERE aa.p_id=pid AND 
                        v.p_id=pid AND 
                        aa.akarni_date>=cyear AND 
                        aa.akarni_date<=thedt 
                GROUP BY aa.vera_id;

    END IF;

END $$

DELIMITER;

这怎么可能,我可以随时间计算此查询以获取不同的表数据

0 个答案:

没有答案