count(*) 查询

问题描述 投票:0回答:1

我尝试解决 Hackerrank 中的问题代码。 An OCCUPATIONS table that contains the following records:

这里是样本输出: 阿什莉(P) 克里斯汀(P) 简(A) 珍妮(D) 朱莉娅(A) 凯蒂(P) 玛丽亚(A) 米拉(S) 普里亚(S) 萨曼莎(D) 共有医生2名。 共有2位歌手。 演员一共有3位。 教授一共有3位。

为什么该代码不起作用

select Concat(Name, '(', Substring(Occupation,1,1), ')') as pr
from Occupations
order by name;

select Concat('There are a total of',' ', count(*),' ',Occupation, 's.') as pr
from Occupations
group by Occupation
order by pr
sql mysql count
1个回答
0
投票

你解决问题的方法很好。你可以使用这种方法,它在 mysql 中有效,但在 oracle 中不起作用。 因为在 Oracle 中,CONCAT 函数最多可以接受 2 个参数来连接,而在 mysql 中,CONCAT 函数可以接受多个参数。 您可以使用嵌套 concat 语句,如下所示:

select concat(concat('共有', count(*)), concat(' ', 占用)) as pr 从职业 按职业分组 按 pr 订购;

否则,使用 ||连接多个字符串,如下所示:

select('共有'|| count(*) || ' ' || 职业|| ') as pr 从职业 按职业分组 按 pr 订购;

© www.soinside.com 2019 - 2024. All rights reserved.