gpt4 book ai didi

mysql : select count when id have multiple same value and calculate percentage

转载 作者:行者123 更新时间:2023-11-29 13:20:59 26 4
gpt4 key购买 nike

+------+---------+
| id | object |
+------+---------+
| 1 | 1 |
| 1 | 2 |
| 1 | 3 |
| 1 | 4 |
+------+---------+

我想选择计数 id 具有相同的值,所以结果是,id 1 有 4 个相同的值,使用 SELECT COUNT( * ) as id1 FROM tablename GROUP BY id

+------+
| id1 |
+------+
| 4 |
+------+

我还有另一张 table :

+------+---------+
| id2 | object |
+------+---------+
| 1 | 1 |
| 1 | 2 |
| 1 | 3 |
| 1 | 4 |
| 1 | 5 |
| 1 | 6 |
| 2 | 1 |
| 2 | 2 |
| 2 | 3 |
| 2 | 4 |
| 2 | 5 |
+------+---------+

如何选择结果的计数,但我仍然不知道如何计算百分比,

+------+---------+
| id1 | id2 |
+------+---------+
| 4 | 6 | # 66%
| 4 | 5 | # 80%
+------+---------+

谢谢大师的帮助。

最佳答案

请尝试这个sqlFiddle

SELECT T1.id as T1ID,
T2.id as T2ID,
T1.id1,
T2.id2,
T1.id1/T2.id2 * 100 as percentage
FROM
(SELECT id,count(*) as id1
FROM tableOne
GROUP BY id
)T1,
(SELECT id,count(*) as id2
FROM tableTwo
GROUP BY id
)T2

关于mysql : select count when id have multiple same value and calculate percentage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20851048/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com