在没有的情况下编写选择查询的最佳做法是什么。需要动态连接和条件

时间:2016-01-07 12:51:17

标签: mysql sql database

我正在使用动态查询概念根据输入参数动态构建我的查询到我的存储过程。 基于输入我必须决定

  1. 动态地我需要确定需要从哪个表中获取列值?
  2. 需要申请哪些JOINS?
  3. 我的条件是什么(基于过滤条件)?
  4. 我正在分享一个示例代码段,用于我正在使用的方法。如下所示

    DELIMITER $$
    
    DROP PROCEDURE IF EXISTS `EMS_teamBasedUserInfo_for_selectedArtifact`$$
    
    CREATE  PROCEDURE `EMS_teamBasedUserInfo_for_selectedArtifact`(
    IN IN_org_type_id BIGINT,
    IN IN_org_id BIGINT,
    IN IN_artifact_type_id VARCHAR(100), 
    IN IN_artifact_id VARCHAR(100), 
    IN IN_artifact_id_list VARCHAR(200), 
    IN IN_outer_delimiter VARCHAR(2), 
    IN IN_inner_delimiter VARCHAR(2), 
    IN IN_team_Id BIGINT)
    
    BEGIN  
            DECLARE VAR_artifact_type VARCHAR(20);
            DECLARE VAR_artifact_ids VARCHAR(20);
            DECLARE VAR_common_column VARCHAR(20000);
            DECLARE VAR_selective_column VARCHAR(20000);
            DECLARE VAR_common_join VARCHAR(20000);
            DECLARE VAR_selective_join VARCHAR(20000);
            DECLARE VAR_common_condition VARCHAR(20000);
            DECLARE VAR_selective_condition VARCHAR(20000);
    
            SET VAR_common_column = CONCAT('SELECT ', 
                                                'Users_Orgs.userId,',
                                                'User_.screenName,',
                                                'User_.firstName,',
                                                'User_.middleName,',
                                                'User_.lastName,',
                                                'User_.emailAddress');
            SET VAR_common_join = CONCAT('FROM ',
                                            'Users_Orgs ',
                                        'INNER JOIN User_ ',
                                            'ON(Users_Orgs.userId = User_.userId)');
            SET VAR_common_condition = CONCAT('WHERE ', 
                                                'organizationId = ', IN_org_id);
    
            IF IN_team_Id > 0 THEN
                SET VAR_selective_join = CONCAT(VAR_selective_join,
                                                ' INNER JOIN Users_Teams ',
                                                    'ON(Users_Orgs.userId = Users_Teams.userId)');
                SET VAR_selective_condition = CONCAT(VAR_selective_join,
                                                ' AND Users_Teams.teamId = ', IN_team_Id);
            END IF;
    
            SELECT typeName INTO VAR_org_type_name FROM V1_OrganizationType WHERE organizationTypeId = IN_org_type_id;
            SELECT CASE WHEN COUNT(NAME)>0 THEN NAME ELSE '-1' END INTO VAR_artifact_type FROM ApplicationParamValue WHERE appParamValueId = CAST(IN_artifact_type_id AS UNSIGNED);
    
    
            IF UPPER(VAR_org_type_name) = UPPER('IMS') THEN
    
                IF UPPER(VAR_artifact_type) = UPPER('Project') THEN
    
                    IF IN_team_Id > 0 THEN
                        SET VAR_selective_join = CONCAT(VAR_selective_join,
                                                        ' INNER JOIN EMS_artifact_team_mapping ',
                                                            'ON(Users_Teams.teamId = EMS_artifact_team_mapping.teamId)');
                        SET VAR_selective_condition = CONCAT(VAR_selective_join,
                                                        ' AND EMS_artifact_team_mapping.teamId = ', IN_team_Id,
                                                        ' AND EMS_artifact_team_mapping.artifactTypeId = ', (CAST(IN_artifact_type_id AS UNSIGNED)));
    
                        IF (CAST(IN_artifact_id AS UNSIGNED)) > 0 THEN
                            SET VAR_selective_condition = CONCAT(VAR_selective_join,
                                                            ' AND EMS_artifact_team_mapping.artifactId = ', (CAST(IN_artifact_id AS UNSIGNED)));
                        END IF;
                    END IF;
                ELSEIF UPPER(VAR_artifact_type) = UPPER('-1') THEN
                    -- SET VAR_artifact_ids = CONCAT(SELECT GROUP_CONCAT(project_id) FROM EMS_project WHERE organizationId = IN_org_id);
                END IF;
            ELSEIF UPPER(VAR_org_type_name) = UPPER('GMS') THEN
    
                IF UPPER(VAR_artifact_type) = UPPER('Project') THEN
    
                    IF IN_team_Id > 0 THEN
                        SET VAR_selective_join = CONCAT(VAR_selective_join,
                                                        ' INNER JOIN EMS_artifact_team_mapping ',
                                                            'ON(Users_Teams.teamId = EMS_artifact_team_mapping.teamId)');
                        SET VAR_selective_condition = CONCAT(VAR_selective_join,
                                                        ' AND EMS_artifact_team_mapping.teamId = ', IN_team_Id,
                                                        ' AND EMS_artifact_team_mapping.artifactTypeId = ', (CAST(IN_artifact_type_id AS UNSIGNED)));
    
                        IF (CAST(IN_artifact_id AS UNSIGNED)) > 0 THEN
                            SET VAR_selective_condition = CONCAT(VAR_selective_join,
                                                            ' AND EMS_artifact_team_mapping.artifactId = ', (CAST(IN_artifact_id AS UNSIGNED)));
                        END IF;
                    END IF;
                ELSEIF UPPER(VAR_artifact_type) = UPPER('-1') THEN
                    -- SET VAR_artifact_ids = CONCAT(SELECT GROUP_CONCAT(project_id) FROM EMS_project WHERE organizationId = IN_org_id);
                END IF;
            END IF;
    
    
            SET @SQL := CONCAT(VAR_common_column, ' ', VAR_selective_column, ' ', VAR_common_join, ' ', VAR_selective_join, ' ', VAR_common_condition, ' ', VAR_selective_condition);
    
            -- select @SQL;
            PREPARE stmt FROM @SQL; 
    
            EXECUTE stmt;
    END$$
    
    DELIMITER ;
    

    我想知道最佳方法是什么?还有其他更好的方法吗?

0 个答案:

没有答案