Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 在两个不同的列中交替使用相同的值,从两行中删除一行_Mysql_Sql_Database_Rdbms - Fatal编程技术网

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;