Mysql 在两个不同的列中交替使用相同的值,从两行中删除一行
查询如下所示:Mysql 在两个不同的列中交替使用相同的值,从两行中删除一行,mysql,sql,database,rdbms,Mysql,Sql,Database,Rdbms,查询如下所示: select g1.gen_id as 'gen_1', g2.gen_id as 'gen_2', count(*) as 'count' from gen g1, gen g2, dir d where g1.gen_id <> g2.gen_id [other irrelevant where conditions here] order by g1.gen_id, g2.gen_id; 如您所见,出现这种情况是因为我得到了同一个表的笛卡尔积(我在from子句
select g1.gen_id as 'gen_1', g2.gen_id as 'gen_2', count(*) as 'count'
from gen g1, gen g2, dir d
where g1.gen_id <> g2.gen_id
[other irrelevant where conditions here]
order by g1.gen_id, g2.gen_id;
如您所见,出现这种情况是因为我得到了同一个表的笛卡尔积(我在from
子句中有两次)。如果您注意到在输出中,我有两列交替的值(前两列-第三列在这里不相关)。我想要的是删除这些重复项中的每一行。我没有粘贴整个输出,但请放心,这就是发生的情况。我有442行输出时,他们应该是221。我想删除“重复”行。有没有办法做到这一点,因为我目前找不到解决办法。使用
where g1.gen_id > g2.gen_id
或
其中g1.gen\u id
.使用
where g1.gen_id > g2.gen_id
或
其中g1.gen\u id
.解决方案是使用
解决方案是使用
where g1.gen_id < g2.gen_id
select g1.gen_id as gen_1, g2.gen_id as gen_2, count(*) as cnt
from gen g1 join
gen g2, dir d
on g1.gen_id < g2.gen_id
where [other irrelevant where conditions here]
order by g1.gen_id, g2.gen_id;