0

我不是 sql 专家。

我想实现一个相当于这个条件的查询

If  ( (first-access-of-error1 = first-access-of-error2 and 
       second-access-of-error1 = second-access-of-error2) OR 
      (first-access-of-error1 = second-access-of-error2 and
       second-accessr-of-error1 = first-access-of-error2) )

我尝试了类似的方法:

select d.id,
       concat(a.variable_name,"|",a.file_url,"|",a.line_number,"|",a.stacktrace)
             as FirstAccess_Params, 
       concat(b.variable_name,"|",b.file_url,"|",b.line_number,"|",b.stacktrace) 
             as SecondA_Params 
from defect d 
right join (accessor a, accessor b) 
   on (d.id=a.defect_id and d.id=b.defect_id and a.id<b.id) 
where d.category_id=0 and d.relationship_id!=-1 
group by FirstAccess_Params,SecondA_Params

通过上面的查询,我可以解决这个问题:

(first-access-of-error1 = first-access-of-error2 and 
 second-access-of-error1 = second-access-of-error2)

但是我在如何完成这个条件时遇到了麻烦:

(first-access-of-error1 = second-access-of-error2 and
 second-accessr-of-error1 = first-access-of-error2)

任何帮助表示赞赏?欢迎任何想法..

这是访问器表的架构

<createTable tableName="accessor">
        <column autoIncrement="true" name="id" type="BIGINT UNSIGNED">
            <constraints nullable="false" primaryKey="true"/>
        </column>
        <column name="defect_id" type="BIGINT UNSIGNED">
            <constraints nullable="false"/>
        </column>
        <column name="operation" type="TINYINT UNSIGNED"/>
        <column name="variable_name" type="VARCHAR(128)"/>
        <column name="object_address" type="VARCHAR(64)"/>
        <column name="type" type="TINYINT UNSIGNED"/>
        <column name="module_id" type="INT UNSIGNED">
            <constraints nullable="false"/>
        </column>
        <column name="file_url" type="VARCHAR(256)"/>
        <column name="function_name" type="VARCHAR(64)"/>
        <column name="line_number" type="SMALLINT UNSIGNED"/>
        <column name="accessing_order" type="TINYINT UNSIGNED"/>
        <column name="stacktrace_type" type="TINYINT UNSIGNED"/>
        <column name="stacktrace" type="VARCHAR(2048)"/>
        <column name="parameter" type="VARCHAR(5120)"/>
    </createTable>

这是缺陷表的模式

<createTable tableName="defect">
        <column autoIncrement="true" name="id" type="BIGINT UNSIGNED">
            <constraints nullable="false" primaryKey="true"/>
        </column>
        <column defaultValueNumeric="0" name="rule_id" type="SMALLINT UNSIGNED">
            <constraints nullable="false"/>
        </column>
        <column defaultValueBoolean="false" name="hide" type="BIT">
            <constraints nullable="false"/>
        </column>
        <column defaultValueNumeric="0" name="relationship_id" type="BIGINT">
            <constraints nullable="false"/>
        </column>
        <column name="category_id" type="SMALLINT UNSIGNED">
            <constraints nullable="false"/>
        </column>
        <column name="sub_category_id" type="SMALLINT UNSIGNED">
            <constraints nullable="false"/>
        </column>
        <column name="module1_id" type="INT UNSIGNED">
            <constraints nullable="false"/>
        </column>
        <column name="module2_id" type="INT UNSIGNED"/>
        <column name="execution_instance_id" type="INT UNSIGNED">
            <constraints nullable="false"/>
        </column>
        <column name="application_id" type="INT UNSIGNED">
            <constraints nullable="false"/>
        </column>
        <column name="project_id" type="INT UNSIGNED">
            <constraints nullable="false"/>
        </column>
        <column name="target_id" type="INT UNSIGNED">
            <constraints nullable="false"/>
        </column>
        <column name="testsuite_id" type="INT UNSIGNED">
            <constraints nullable="false"/>
        </column>
        <column name="timestamp" type="INT UNSIGNED">
            <constraints nullable="false"/>
        </column>
        <column name="priority" type="BIT"/>
        <column name="status" type="BIT"/>
        <column name="assignee" type="VARCHAR(64)"/>
        <column name="label_ids" type="VARCHAR(1024)"/>
        <column name="remark" type="VARCHAR(512)"/>
        <column name="accessor_ids" type="VARCHAR(1024)"/>
        <column name="parameter" type="VARCHAR(2048)"/>
        <column defaultValueNumeric="0" name="parent_id" type="BIGINT">
            <constraints nullable="false"/>
        </column>
    </createTable>
4

2 回答 2

0

考虑这个 jsFiddle:http ://sqlfiddle.com/#!2/c25e1/1 。它不显示重复项。还要考虑使用 DISTINCT sql 命令。

于 2013-06-25T10:27:52.137 回答
0

为了回答的完整性:

完成第一个条件:

(first-access-of-error1 = first-access-of-error2 and 
   second-access-of-error1 = second-access-of-error2)

我的代码有点正确。在这里查看类似的东西https://stackoverflow.com/a/347300/1229355

具有挑战性的部分是完成第二个条件:

(first-access-of-error1 = second-access-of-error2 and

错误的第二个访问者 1 = 错误的第一个访问者 2)

为此:我创建了一个新表。尽管我从视图开始,然后移至临时表,然后最终移至表。有了视图,我在性能上遇到了麻烦。由于查询花费了大量时间来复制 tmp 表。使用临时表,我不能在一个查询中多次使用它,所以这不是我的选择。所以我只能坚持使用桌子。

像这样创建表:

create table myDraceView (index(id)) as select d.id,concat_ws('|',a.variable_name,a.file_url,a.line_number,a.stacktrace) as FirstAccess_Params,"
                   + "concat_ws('|',b.variable_name,b.file_url,b.line_number,b.stacktrace) as SecondA_Params from defect d right join (accessor a, accessor b) on (d.id=a.defect_id and d.id=b.defect_id and a.id<b.id) where d.category_id=0 and d.relationship_id!=-1 and d.defect_level='"
                   + type
                   + "' and "
                   + mdaCondition;

(不好意思赶时间。。没时间正式写查询。。。以后再写吧)

然后我做了:

"select ids from (select concat( if( e1.id <= e2.id, e1.id, e2.id ),"
                   + CONCAT_SEP
                   + ",if( e1.id > e2.id, e1.id, e2.id ) ) as ids,e1.FirstAccess_Params as e1_FA,e1.SecondA_Params as e1_SA from myDraceView e1 join myDraceView e2 where e1.FirstAccess_Params=e2.SecondA_Params and e1.SecondA_Params=e2.FirstAccess_Params and e1.id!=e2.id group by e1.FirstAccess_Params,e1.SecondA_Params ) as l group by ids";

如果有人看到我可以做出的任何可能的改进,欢迎提出任何建议。

于 2013-06-28T15:51:58.617 回答