我想在名称列表中查找重复名称的数量。 就像
names
------
jhon
jhon
jack
jhon
jack
我执行了以下查询以计算名称的出现次数
select count(names) as namejhon from table_team where names=‘jhon’
select count(names) as namejack from table_team where names=‘jack’
据此,我想获得下表(期望的结果)
namejhon namejack
----------- -----------
3 2
1 record(s) selected.
,但是在使用 JOIN 时会得到此提示
namejack
-----------
5
1 record(s) selected.
我该怎么做才能获得理想的结果?任何指导表示赞赏。
答案 0 :(得分:1)
以下代码将为您提供所需的信息,但这只是查询而不是表。如果您确实想创建一个表,其中的列/字段名称是人民的名字,而行/记录只是计数,则应查看CREATE TABLE文档:https://dev.mysql.com/doc/refman/5.5/en/creating-tables.html和INSERT命令文档: https://dev.mysql.com/doc/refman/8.0/en/insert.html。
SELECT
COUNT(names) AS COUNT,
names
FROM
table_team
WHERE
names in ('jhon', 'jack')
GROUP BY
names
自原始海报添加新信息后:
SELECT
*
FROM
(SELECT COUNT(*) AS nameJack FROM table_team WHERE `names` = 'jack') AS SUB1
INNER JOIN
(SELECT COUNT(*) AS nameJhon FROM table_team WHERE `names` = 'jhon') AS SUB2
ON
(
SUB1.`nameJack` <> SUB2.`nameJhon`
OR
SUB1.`nameJack` = SUB2.`nameJhon`
)
答案 1 :(得分:0)
对于MySQL:
使用两个子选择。
from memory cache
如果要实现,请使用SELECT (SELECT count(names)
FROM table_team
WHERE names = 'jhon') namejhon,
(SELECT count(names)
FROM table_team
WHERE names = 'jack') namejack;
snytax。
CREATE TABLE ... AS SELECT ...
我不了解DB2。但这也应该为您提供指示。