如何在不丢失查询信息的情况下混合COUNT()和非COUNT()列?

时间:2009-01-13 16:26:22

标签: sql

我从查询开始:

SELECT strip.name as strip, character.name as character
  from strips, characters, appearances
 where strips.id = appearances.strip_id
   and characters.id = appearances.character.id
   and appearances.date in (...)

这让我得到了一些结果:

strip                 | character
'Calvin & Hobbes'     | 'Calvin'
'Calvin & Hobbes'     | 'Hobbes'
'Pearls Before Swine' | 'Pig'
'Pearls Before Swine' | 'Rat'
'Pearls Before Swine' | 'Hobbes'  # a guest appearance
'Pearls Before Swine' | 'Calvin'  # a guest appearance

然后我想得到结果集中使用字符(在任何条带中)的次数的COUNT。所以我试过了:

SELECT count(character.id), strip.name as strip, character.name as character
  from strips, characters, appearances
 where strips.id = appearances.strip_id
   and characters.id = appearances.character.id
   and appearances.date in (...)

但那给了我

[ERROR 11:20:17] Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause

所以我试过了:

SELECT count(character.id), strip.name as strip, character.name as character
  from strips, characters, appearances
 where strips.id = appearances.strip_id
   and characters.id = appearances.character.id
   and appearances.date in (...)
 group by character.id

哪个给了我

count | strip                 | character
4     | 'Calvin & Hobbes'     | 'Calvin'
4     | 'Calvin & Hobbes'     | 'Hobbes'
2     | 'Pearls Before Swine' | 'Pig'
2     | 'Pearls Before Swine' | 'Rat'

也就是说,我丢失了所有关于哪些字符出现在哪些条带中的额外信息。

我想得到的是:

count | strip                 | character
4     | 'Calvin & Hobbes'     | 'Calvin'
4     | 'Calvin & Hobbes'     | 'Hobbes'
2     | 'Pearls Before Swine' | 'Pig'
2     | 'Pearls Before Swine' | 'Rat'
4     | 'Pearls Before Swine' | 'Calvin'
4     | 'Pearls Before Swine' | 'Hobbes'

但我似乎无法弄明白。如果重要,我会使用MySQL。也许只需要两次查询。

6 个答案:

答案 0 :(得分:3)

mySQL是否支持分析功能?像:

SELECT foo.bar, baz.yoo, count(baz.yoo) over (partition by foo.bar) as yoo_count 
from foo, bar
where foo.baz_id = baz.id and baz.id in (...)

可替换地:

SELECT foo.bar, baz.yoo, v.yoo_count 
from foo, bar, 
( select foo.baz_id, count(*) as yoo_count
  from foo
  group by foo.baz_id
) as v
where foo.baz_id = baz.id and baz.id in (...)
and v.baz_id = foo.baz_id;

答案 1 :(得分:1)

如何按foo.bar分组?

SELECT count(baz.id) as count, foo.bar, baz.yoo where foo.baz_id = baz.id and baz.id in (...) group by foo.bar

答案 2 :(得分:0)

在RDBMS系统上,我将计数查询选择到临时表中,然后将临时表重新加入主表。这将为您提供两全其美的优势。

我不知道mySQL是否支持临时表。

答案 3 :(得分:0)

如果MySQL支持分析/窗口功能,则:

select bar, yoo,
    count(yoo) over (partition by yoo) c 
from t
/

否则你需要使用相关的子查询:

select bar, yoo,
    (select count(yoo) from t t2 where t2.yoo=t.yoo) c 
from t
/

在oracle上,测试数据如下:

create table t(bar number, yoo varchar2(16));

insert into t(bar, yoo) values (1, 'hello');

insert into t(bar, yoo) values (2, 'goodbye');

insert into t(bar, yoo) values (3, 'goodbye');

insert into t(bar, yoo) values (4, 'goodbye');

insert into t(bar, yoo) values (2, 'calvin');

insert into t(bar, yoo) values (5, 'Hobbes');

insert into t(bar, yoo) values (6, 'Hobbes');

commit;

答案 4 :(得分:0)

子选择:

SELECT foo.bar
    ,baz.yoo
    ,(SELECT COUNT(*) FROM baz AS baz2 WHERE baz2.yoo = baz.yoo etc.) AS yoo_count
FROM foo, baz
WHERE foo.baz_id = baz.id
    AND baz.id in (...)

您也可以进行子选择并将其嵌套并加入。

答案 5 :(得分:0)

如果我要合理地要求这样的计数字段,那么我创建一个用户定义的函数来返回值,并在正常选择中使用它。