首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SELECT语句中的MySQL分组

SELECT语句中的MySQL分组
EN

Stack Overflow用户
提问于 2014-03-18 06:09:34
回答 2查看 64关注 0票数 1

我有一张桌子“动物”

我想弄清楚我在这两类动物中有多少种:爬行动物:螯虾、鳄鱼、蜥蜴、蛇和啮齿动物:仓鼠、豪猪、睡鼠、水豚。

当我为一个类别写作时:

代码语言:javascript
复制
SELECT Reptiles
FROM    (Select distinct COUNT(an_id) as Reptiles
         from animals
         Where an_type in ('chelonian', 'crocodilian', 'lizard', 'snake'))rep;

我得到了正确的答案:

爬行动物=9

然后我想添加第二栏的啮齿动物,所以我写道:

代码语言:javascript
复制
SELECT Reptiles, Rodents
FROM    (Select distinct COUNT(an_id) as Reptiles
        from animals
        Where an_type in ('chelonian', 'crocodilian', 'lizard', 'snake'))rep
            (Select distinct count(an_id) as Rodents
            from animals
            where an_type in ('hamster', 'porcupine', 'dormouse', 'capybara'))rod
;

当然,我会收到一个语法错误。

我只想有两栏爬行动物和啮齿类动物的数量(an_id)。有人能告诉我写这个查询的正确方法吗?

提前谢谢你,

滴滴

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-03-18 06:14:12

您在分隔表时忽略了逗号(,)

an_type in (“螯合”,“鳄鱼”,“蜥蜴”,“蛇”)代表 (选择不同计数(An_id)作为啮齿动物)

试着像这样

代码语言:javascript
复制
SELECT Reptiles, Rodents
FROM 
    (
     Select distinct COUNT(an_id) as Reptiles
     From animals
     Where an_type in ('chelonian', 'crocodilian', 'lizard', 'snake')
    ) rep,
    (
     Select distinct count(an_id) as Rodents
     from animals
     where an_type in ('hamster', 'porcupine', 'dormouse', 'capybara')
    )rod
票数 1
EN

Stack Overflow用户

发布于 2014-03-18 06:26:25

这一行上面缺少逗号(,)吗?

代码语言:javascript
复制
(Select distinct count(an_id) as Rodents
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22471615

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档