3

我有一个包含两列的表格,如下所示:

source_cid     inchikey
----------     --------
 1             qqmn
 2             qqmn
 3             ccmm

现在我想选择具有相同 inchikeys 的 source_cids

这是我的查询:

SELECT source_cid, count(*) as c
FROM inchikey
GROUP BY inchikey HAVING count(*)>1

此代码永远运行。我该如何修改它?

4

2 回答 2

0

尝试在表 inchikey 上的 (source_cid, inchikey) 上创建重复索引,然后尝试运行查询:

SELECT inchikey, group_concat(distinct source_cid) source_cids, count(*) as c
FROM inchikey
GROUP BY inchikey HAVING count(distinct source_cid)>1

(您现有的查询只会为每个重复的 inchikey 显示一个 source_cid。)

于 2013-06-17T15:21:36.053 回答
0

首先,正如 Anigel 所说,我们需要查看您的创建语句,并且您应该使用索引。

其次,您的查询不会显示应显示的所有行。

见:http ://www.sqlfiddle.com/#!2/a810d/7

SELECT source_cid, count(*) as c
FROM inchikey
GROUP BY inchikey HAVING count(*)>1;

不幸的是,仅输出 source_cids 1 的行。

select * from inchikey i,
(
SELECT i2.inchikey, count(i2.source_cid) as c
FROM inchikey i2
GROUP BY i2.inchikey HAVING count(i2.source_cid)>1
 ) as cd
where cd.inchikey = i.inchikey;

这样,输出 1 和 2 的行。

于 2013-06-17T15:21:50.553 回答