+------+---------+
| id | object |
+------+---------+
| 1 | 1 |
| 1 | 2 |
| 1 | 3 |
| 1 | 4 |
+------+---------+
我想选择具有相同值的count id,因此结果是,id 1有4个相同的值,使用SELECT COUNT(*)作为id1 FROM tablename GROUP BY id
+------+
| id1 |
+------+
| 4 |
+------+
我有另一张桌子:
+------+---------+
| 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%
+------+---------+
感谢您的帮助,掌握。
答案 0 :(得分:0)
SELECT COUNT(table1.object) as id1,
COUNT(table2.object) as id2,
COUNT(table1.object) * 100 / COUNT(table2.object) as percentage
FROM table2
left join table1 on table2.id = table1.id
GROUP BY table2.id