纪念一下写过的最长SQL

4 篇文章 0 订阅
		SELECT '1'                               AS apply_type,
               GROUP_CONCAT(c.project_name)      AS project_name,
               GROUP_CONCAT(d.business_nickname) AS business_nickname,
               b.id                              AS apply_id,
               b.`status`,
               b.audit_node,
               e.`name`,
               b.submit_user_id,
               b.submit_time
        FROM (SELECT id, project_id FROM `oa_overtime_apply`) AS a
                 JOIN mysql.help_topic AS b ON b.help_topic_id < (char_length(a.project_id) -
                                                                     char_length(REPLACE(a.project_id, ',', '')) + 1)
                 RIGHT JOIN oa_overtime_apply b ON a.id = b.id
                 RIGHT JOIN oa_overtime_refer f ON b.id = f.apply_id
                 LEFT JOIN oa_project c
                           ON substring_index(substring_index(a.project_id, ',', b.help_topic_id + 1), ',', - 1) =
                              c.project_id
                 LEFT JOIN oa_business d ON c.project_business_id = d.business_id
                 LEFT JOIN oa_staff e ON b.submit_user_id = e.user_id
        WHERE b.STATUS != '0'
        AND f.user_id = #{userId}
        GROUP BY
            b.id
        UNION
        SELECT '2'                               AS apply_type,
               GROUP_CONCAT(c.project_name)      AS project_name,
               GROUP_CONCAT(d.business_nickname) AS business_nickname,
               b.id                              AS apply_id,
               b.`status`,
               b.audit_node,
               e.`name`,
               b.submit_user_id,
               b.submit_time
        FROM (SELECT id, project_id FROM `oa_purchase_apply`) AS a
                 JOIN mysql.help_topic AS b ON b.help_topic_id < (char_length(a.project_id) -
                                                                     char_length(REPLACE(a.project_id, ',', '')) + 1)
                 RIGHT JOIN oa_purchase_apply b ON a.id = b.id
                 RIGHT JOIN oa_purchase_refer f ON b.id = f.apply_id
                 LEFT JOIN oa_project c
                           ON substring_index(substring_index(a.project_id, ',', b.help_topic_id + 1), ',', - 1) =
                              c.project_id
                 LEFT JOIN oa_business d ON c.project_business_id = d.business_id
                 LEFT JOIN oa_staff e ON b.submit_user_id = e.user_id
        WHERE b.STATUS != '0'
        AND f.user_id = #{userId}
        GROUP BY
            b.id
        UNION
        SELECT '3'                               AS apply_type,
               GROUP_CONCAT(c.project_name)      AS project_name,
               GROUP_CONCAT(d.business_nickname) AS business_nickname,
               b.id                              AS apply_id,
               b.`status`,
               b.audit_node,
               e.`name`,
               b.submit_user_id,
               b.submit_time
        FROM (SELECT id, project_id FROM `oa_cost_apply`) AS a
                 JOIN mysql.help_topic AS b ON b.help_topic_id < (char_length(a.project_id) -
                                                                     char_length(REPLACE(a.project_id, ',', '')) + 1)
                 RIGHT JOIN oa_cost_apply b ON a.id = b.id
                 RIGHT JOIN oa_cost_refer f ON b.id = f.apply_id
                 LEFT JOIN oa_project c
                           ON substring_index(substring_index(a.project_id, ',', b.help_topic_id + 1), ',', - 1) =
                              c.project_id
                 LEFT JOIN oa_business d ON c.project_business_id = d.business_id
                 LEFT JOIN oa_staff e ON b.submit_user_id = e.user_id
        WHERE b.STATUS != '0'
        AND f.user_id = #{userId}
        GROUP BY
            b.id
        UNION
        SELECT '4'                               AS apply_type,
               GROUP_CONCAT(c.project_name)      AS project_name,
               GROUP_CONCAT(d.business_nickname) AS business_nickname,
               b.id                              AS apply_id,
               b.`status`,
               b.audit_node,
               e.`name`,
               b.submit_user_id,
               b.submit_time
        FROM (SELECT id, project_id FROM `oa_reimburse_apply`) AS a
                 JOIN mysql.help_topic AS b ON b.help_topic_id < (char_length(a.project_id) -
                                                                     char_length(REPLACE(a.project_id, ',', '')) + 1)
                 RIGHT JOIN oa_reimburse_apply b ON a.id = b.id
                 RIGHT JOIN oa_reimburse_refer f ON b.id = f.apply_id
                 LEFT JOIN oa_project c
                           ON substring_index(substring_index(a.project_id, ',', b.help_topic_id + 1), ',', - 1) =
                              c.project_id
                 LEFT JOIN oa_business d ON c.project_business_id = d.business_id
                 LEFT JOIN oa_staff e ON b.submit_user_id = e.user_id
        WHERE b.STATUS != '0'
        AND f.user_id = #{userId}
        GROUP BY
            b.id
        UNION
        SELECT '5'                               AS apply_type,
               GROUP_CONCAT(c.project_name)      AS project_name,
               GROUP_CONCAT(d.business_nickname) AS business_nickname,
               b.id                              AS apply_id,
               b.`status`,
               b.audit_node,
               e.`name`,
               b.submit_user_id,
               b.submit_time
        FROM (SELECT id, project_id FROM `oa_outwork_apply`) AS a
                 JOIN mysql.help_topic AS b ON b.help_topic_id < (char_length(a.project_id) -
                                                                     char_length(REPLACE(a.project_id, ',', '')) + 1)
                 RIGHT JOIN oa_outwork_apply b ON a.id = b.id
                 RIGHT JOIN oa_outwork_refer f ON b.id = f.apply_id
                 LEFT JOIN oa_project c
                           ON substring_index(substring_index(a.project_id, ',', b.help_topic_id + 1), ',', - 1) =
                              c.project_id
                 LEFT JOIN oa_business d ON c.project_business_id = d.business_id
                 LEFT JOIN oa_staff e ON b.submit_user_id = e.user_id
        WHERE b.STATUS != '0'
        AND f.user_id = #{userId}
        GROUP BY
            b.id
        UNION
        SELECT '6'                               AS apply_type,
               GROUP_CONCAT(c.project_name)      AS project_name,
               GROUP_CONCAT(d.business_nickname) AS business_nickname,
               b.id                              AS apply_id,
               b.`status`,
               b.audit_node,
               e.`name`,
               b.submit_user_id,
               b.submit_time
        FROM (SELECT id, project_id FROM `oa_trip_apply`) AS a
                 JOIN mysql.help_topic AS b ON b.help_topic_id < (char_length(a.project_id) -
                                                                     char_length(REPLACE(a.project_id, ',', '')) + 1)
                 RIGHT JOIN oa_trip_apply b ON a.id = b.id
                 RIGHT JOIN oa_trip_refer f ON b.id = f.apply_id
                 LEFT JOIN oa_project c
                           ON substring_index(substring_index(a.project_id, ',', b.help_topic_id + 1), ',', - 1) =
                              c.project_id
                 LEFT JOIN oa_business d ON c.project_business_id = d.business_id
                 LEFT JOIN oa_staff e ON b.submit_user_id = e.user_id
        WHERE b.STATUS != '0'
        AND f.user_id = #{userId}
        GROUP BY
            b.id
        UNION
        SELECT '7'                               AS apply_type,
               GROUP_CONCAT(c.project_name)      AS project_name,
               GROUP_CONCAT(d.business_nickname) AS business_nickname,
               b.id                              AS apply_id,
               b.`status`,
               b.audit_node,
               e.`name`,
               b.submit_user_id,
               b.submit_time
        FROM (SELECT id, project_id FROM `oa_settle_apply`) AS a
                 JOIN mysql.help_topic AS b ON b.help_topic_id < (char_length(a.project_id) -
                                                                     char_length(REPLACE(a.project_id, ',', '')) + 1)
                 RIGHT JOIN oa_settle_apply b ON a.id = b.id
                 RIGHT JOIN oa_settle_refer f ON b.id = f.apply_id
                 LEFT JOIN oa_project c
                           ON substring_index(substring_index(a.project_id, ',', b.help_topic_id + 1), ',', - 1) =
                              c.project_id
                 LEFT JOIN oa_business d ON c.project_business_id = d.business_id
                 LEFT JOIN oa_staff e ON b.submit_user_id = e.user_id
        WHERE b.STATUS != '0'
        AND f.user_id = #{userId}
        GROUP BY
            b.id
        ORDER BY
            submit_time DESC
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值